Monday, February 9, 2009

Count total occurrence of a pattern - awk

Lets see how we can use awk FS variable to count the total occurrence of a pattern (word or character) in a file.

Input file:
$ cat file.txt
bash scripting bash
,bash, awk
sed,expect, python|awk

Required: Count how many times the word "bash" is occurred in the above file.

Awk solution:

$ awk -F "bash" '{print NF-1}' file.txt

Now adding them:

$ awk -F "bash" '{print NF-1}' file.txt | awk '{s+=$0} END {print s}'

So combining:

$ awk -F "bash" '{s+=(NF-1)} END {print s}' file.txt

So the word "bash" has occurred 3 times in the above file.

The default field-separator (FS) of awk is a tab or spaces. Using the word or character as the FS, we can count the total occurrence of that word or character in the above way.

1 comment:

Murpholinox Peligro said...

It works but it is more complicated than; grep "pattern" filename |wc -l

© Jadu Saikia