Computer Hope
Software => BSD, Linux, and Unix => Topic started by: aggressivebloodcell on June 26, 2007, 04:39:38 PM
-
Hey all,
I need some help using grep. How would I do a word count for specific words from a file. Lets say I am searching for apples, bannanas and grapes from a text file and need to output the frequency of those words. I do not want to do a grep of those words one by one.
Much Appreciated,
abc
-
grep only count lines that contains your word. If two identical words are on the same line, it will still count as 1.
you might want to use awk.
awk 'BEGIN{FS=",";c=0; search="word"}
$0~search{ for (i=1;i<=NF;i++) {
if ($i == search) {
c++
}
}
} END{print "Total count of "search " is: "c}' file