Counting number of character occurrences per line

onlyf

I have a file that looks like this: ( Note : A*, B*, C* are placeholders). The file is delimited by ;

AAAA;BBBB;CCCCCCCC;DD;EEEEEEEE;FF;
AAA1;BBBBB;CCCC;DD;EEEEEEEE;FFFFF;
AAA3;BB;CCCC;DDDDDDDDD;EEEEEEE;FF;

I m trying to write a small script that counts the number of occurrences of the delimiter ; and if it is lesser or greater than 5, output said line to a text file.

delim=";"

while read line
do  
    n_of_occ=$(grep -o "$delim" <<< "$line" | wc -l)

    if [[ $n_of_occ < 5 ]] || [[ $n_of_occ > 5 ]]
    then
        echo $line >> outfile
    fi
done

For some reason, this doesn't seem to work and my output is garbled. Could someone assist or provide a different way to tackle this? Perhaps with Perl instead of bash?

Borodin

Unfortunately every line in your sample data has six semicolons, which means they should all be printed. However, here is a one-line Perl solution

$ perl -ne'print if tr/;// != 5' aaa.csv
AAAA;BBBB;CCCCCCCC;DD;EEEEEEEE;FF;
AAA1;BBBBB;CCCC;DD;EEEEEEEE;FFFFF;
AAA3;BB;CCCC;DDDDDDDDD;EEEEEEE;FF;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

counting number of occurrences per user in google sheets

From Dev

Counting number of occurrences recursively

From Dev

Automate counting the number of occurrences

From Dev

Counting number of occurrences in R

From Dev

Recursively counting character occurrences in a string

From Dev

counting the number of characters per line from a text file in C

From Dev

counting the occurrences of a field on a individual line

From Dev

Counting the number of digit occurrences in a string

From Dev

Counting number of occurrences of word in java

From Dev

Collecting Items and Counting the Number of Occurrences

From Dev

Collecting Items and Counting the Number of Occurrences

From Dev

Counting number of occurrences in several files

From Dev

Quickly redistribute number of words/character per line in VIM

From Dev

Counting the Occurrences of a User Specified Character in a String

From Dev

Counting All Character Occurrences in a Text File in C

From Dev

Counting occurrences of a character in a string within a cell

From Dev

Count text occurrences per line

From Dev

Getting number of occurrences per year

From Dev

UNIX - Count occurrences of character per line between two fields and add new column with result

From Dev

Counting the number of each character in a file

From Java

Count the number occurrences of a character in a string

From Dev

Find a number of occurrences of character in string

From Dev

Find a number of occurrences of character in string

From Dev

Counting number of occurrences in Foreign Key relationships

From Dev

Counting the number of occurrences of a substring within a string in PostgreSQL

From Dev

Comparing two text files and counting number of occurrences

From Dev

Counting the number of occurrences of each element in Matlab

From Dev

Counting the number of occurrences of an element in an array using recursion

From Dev

Counting the number of specific occurrences in a java String

Related Related

  1. 1

    counting number of occurrences per user in google sheets

  2. 2

    Counting number of occurrences recursively

  3. 3

    Automate counting the number of occurrences

  4. 4

    Counting number of occurrences in R

  5. 5

    Recursively counting character occurrences in a string

  6. 6

    counting the number of characters per line from a text file in C

  7. 7

    counting the occurrences of a field on a individual line

  8. 8

    Counting the number of digit occurrences in a string

  9. 9

    Counting number of occurrences of word in java

  10. 10

    Collecting Items and Counting the Number of Occurrences

  11. 11

    Collecting Items and Counting the Number of Occurrences

  12. 12

    Counting number of occurrences in several files

  13. 13

    Quickly redistribute number of words/character per line in VIM

  14. 14

    Counting the Occurrences of a User Specified Character in a String

  15. 15

    Counting All Character Occurrences in a Text File in C

  16. 16

    Counting occurrences of a character in a string within a cell

  17. 17

    Count text occurrences per line

  18. 18

    Getting number of occurrences per year

  19. 19

    UNIX - Count occurrences of character per line between two fields and add new column with result

  20. 20

    Counting the number of each character in a file

  21. 21

    Count the number occurrences of a character in a string

  22. 22

    Find a number of occurrences of character in string

  23. 23

    Find a number of occurrences of character in string

  24. 24

    Counting number of occurrences in Foreign Key relationships

  25. 25

    Counting the number of occurrences of a substring within a string in PostgreSQL

  26. 26

    Comparing two text files and counting number of occurrences

  27. 27

    Counting the number of occurrences of each element in Matlab

  28. 28

    Counting the number of occurrences of an element in an array using recursion

  29. 29

    Counting the number of specific occurrences in a java String

HotTag

Archive