Search code examples
bashawkgrepstructuresubject

How to count the occurrence of an element in each line in a file (bash)


I have a file that looks like this:

1|2|3|4
1|2|3|4
1|2|3
1|2
1|2|3|4
1|2|3|4

what I want to do is to count the frequency of times that | appears in each line and to print a message like this:

4 line(s) have 3 occurrence(s) of |
1 line(s) have 2 occurrence(s) of |
1 line(s) have 1 occurrence(s) of |

I've been using this code grep -o '|' filename | wc -l but it's counting the amount of times that | appears in the whole file and not only in each line. I am new at bash so I would really appreciate your help!


Solution

  • You may use this awk:

    awk -F "|" '{++fq[NF-1]} END {for (f in fq) printf "%d line(s) have %d occurrence(s) of %s\n", fq[f], f, FS}' file
    
    1 line(s) have 1 occurrence(s) of |
    1 line(s) have 2 occurrence(s) of |
    4 line(s) have 3 occurrence(s) of |
    

    To make it more readable:

    awk -F "|" '{
       ++fq[NF-1]
    }
    END {
    for (f in fq)
       printf "%d line(s) have %d occurrence(s) of %s\n", fq[f], f, FS
    }' file