Count the number of times a word appears in a file

LinuxFile

Linux Problem Overview


What is an easy way to count the number of times a word appears in a file?

Linux Solutions


Solution 1 - Linux

This will also count multiple occurrences of the word in a single line:

grep -o 'word' filename | wc -l

Solution 2 - Linux

cat filename | tr ' ' '\n' | grep 'word' | wc -l

Solution 3 - Linux

fgrep "word to be counted" filename|wc -w

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmagqqView Question on Stackoverflow
Solution 1 - Linuxmohit6upView Answer on Stackoverflow
Solution 2 - LinuxAlexander TorstlingView Answer on Stackoverflow
Solution 3 - LinuxvenkateshView Answer on Stackoverflow