in awk how to count the repeat words

December 8, 2010 at 13:34:04
Specs: Windows Vista
i want to count the "ing" in this line using awk

when am going to home. it start to raining, so i am running toward home

i want print how many times "ing" came in the line using awk

it should print 3


See More: in awk how to count the repeat words

Report •

#1
December 8, 2010 at 14:21:36
awk's gsub command returns the number of times "ing" was replaced:

count=gsub("ing","", $0) # count should equal 3


Report •

#2
December 8, 2010 at 16:38:54
thank you

Report •

#3
December 15, 2010 at 21:34:09
If the pattern you are looking for is in multiple lines in the same file, then you just can use the following (slightly modified version of nails):

cat <file name> | awk ' { count=gsub("ing", "", $0); if (count==1) TOTAL+=1; } END { print TOTAL } '


Report •
Related Solutions


Ask Question