Here’s how you can count the total number of word occurrences using grep
on Linux or Unix:
- Open a terminal and navigate to the directory containing the file you want to search.
- Use the following command to count the total number of occurrences of a word:
grep -o -w "<word>" <file> | wc -l
- Replace
<word>
with the word you want to search for. - 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
.