How do I count the number of occurrences of a word in a text file with the command line?
$ tr ' ' '\n' < FILE | grep WORD | wc -l
Where tr
replaces spaces with newlines, grep
filters all resulting lines matching WORD and wc
counts the remaining ones.
One can even save the wc
part using the -c
option of grep:
$ tr ' ' '\n' < FILE | grep -c WORD
The -c
option is defined by POSIX.
If it is not guaranteed that there are spaces between the words, you have to use some other character (as delimiter) to replace. For example alternative tr
parts are
tr '"' '\n'
or
tr "'" '\n'
if you want to replace double or single quotes. Of course, you can also use tr
to replace multiple characters at once (think different kinds of whitespace and punctuation).
In case you need to count WORD but not prefixWORD, WORDsuffix or prefixWORDsuffix, you can enclose the WORD pattern in begin/end-of-line markers:
grep -c '^WORD$'
Which is equivalent to word-begin/end markers, in our context:
grep -c '\<WORD\>'
With GNU grep, this works: grep -o '\<WORD\>' | wc -l
-o
prints each matched parts of each line on a separate line.
\<
asserts the start of a word and \>
asserts the end of a word (similar to Perl's \b
), so this ensures that you're not matching a string in the middle of a word.
For example,
$ python -c 'import this' | grep '\<one\>' There should be one-- and preferably only one --obvious way to do it. Namespaces are one honking great idea -- let's do more of those! $ python -c 'import this' | grep -o '\<one\>' one one one $ python -c 'import this' | grep -o '\<one\>' | wc -l 3
This unfortunately does not work with GNU coreutils
.
grep -o -c WORD file
If it works on your platform, it's an elegant and fairly intuitive solution; but the GNU folks are still thinking.