Counting the Number of Occurrences of a Specific Word in a File

  • Read
  • Discuss

To count the number of occurrences of a specific word in a file in bash, you can use the following command:

grep -o word filename | wc -w

This command uses grep to search the file for occurrences of the word “word”. The -o option tells grep to print only the matching parts of the input lines, which in this case will be the word “word” itself. The output of grep is then piped to wc, which is used to count the number of words (-w option). The final result is the number of occurrences of the word in the file.

Leave a Reply

Scroll to Top