Take the 2-minute tour ×

Is it possible to determine the number of times a particular word appears using grep

I tried the "-c" option but this returns the number of matching lines the particular word appears in

For example if I have a file with

some words and matchingWord and matchingWord

and then another matchingWord

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

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

share|improve this question
add comment

2 Answers

grep -o string file will return all matching occurrences of string. You can then do grep -o string file | wc -l to get the count you're looking for.

share|improve this answer
Do you know if I can ignore the case when I am searching for matching occurrences ... Tried the -i option with the command above but this did not work –  sachin Oct 9 '11 at 7:05
add comment

I think that using grep -i -o string file | wc -l should give you the correct output, what happens when you do grep -i -o string file on the file?

share|improve this answer
add comment

Your Answer


