Determining the number of words with grep (in cases where there are several words in a line)

Is it possible to determine the number of times a certain word appears with grep

I tried the "-c" option, but this returns the number of matching lines that indicates a particular word in

For example, if I have a file with

a few words and matchWord and matchWord

and then another matchWord

running grep in this file for "matchingWord" with the option "-c" will only return 2 ...

note: this is a grep command line utility on standard unix os

+4
source share
3 answers

grep -o string file will return all matching occurrences of the string. Then you can do grep -o string file | wc -l grep -o string file | wc -l to get the bill you are looking for.

+6
source

I think using grep -i -o string file | wc -l should give you the correct conclusion, what happens when you make grep -i -o a string file in a file?

0
source

You can just count the words ( -w ) with the wc program:

 > echo "foo foo" | grep "foo" | wc -w > 2 
0
source

All Articles