How to count total number of word occurrences using grep on Linux or Unix

Here’s how you can count the total number of word occurrences using grep on Linux or Unix:

  1. Open a terminal and navigate to the directory containing the file you want to search.
  2. Use the following command to count the total number of occurrences of a word:
grep -o -w "<word>" <file> | wc -l
  1. Replace <word> with the word you want to search for.
  2. Replace <file> with the name of the file you want to search.

The output of the command will be the total number of occurrences of the specified word in the file.

Note: The -o option tells grep to display only the matching part of each line, the -w option tells grep to match only whole words, and the wc -l command counts the number of lines in the output of grep.

Leave a Comment