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
2
1
0
Now adding them:
$ awk -F "bash" '{print NF-1}' file.txt | awk '{s+=$0} END {print s}'
3
So combining:
$ awk -F "bash" '{s+=(NF-1)} END {print s}' file.txt
3
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.
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
1 comment:
Hi!
It works but it is more complicated than; grep "pattern" filename |wc -l
Post a Comment