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

Viewed 116

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!

3 Answers

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

You can also use bash arrays, with a script like this:

#/bin/bash
# Variables
file="$1"
declare -a results
# Scanning
while IFS='|' read -a line; do
  n=$((${#line[@]} - 1))
  results[$n]=$((${results[$n]} + 1))
done <"$file"
# Printing
for n in ${!results[@]}; do
  echo ${results[$n]} lines\(s\) have $n occurences of \|
done

With your shown samples, could you please try following.

awk '
{
  count[gsub(/\|/,"&")]++
}
END{
  for(i in count){
    print count[i] " line(s) have "i " occurrence(s) of |"
  }
}'  Input_file

Output will be as follows.

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