How to Count Total Number of Occurrences Using grep?

The “grep” command searches the specified word, a pattern of characters, or a string from the file. By default, this utility searches the particular pattern/word from the file and displays it in the terminal. However, it does not display the total number of occurrences in digits of the specific word/pattern. For this operation, the “grep” uses the other built-in command line tools such as “wc(word count)”, and the “tr(translates)”.

This post illustrates how to count the total number of occurrences using the “grep” command.

How to Count the Total Number of Occurrences Using grep?

The “grep” command line tool offers a wide range of supported flags to count the total number of occurrences with the help of practical examples. 

Sample File

A “SampleFile.txt” is taken as an example having the following content displayed in the terminal:

$ cat SampleFile.txt

Let’s carry out some practical implementation to count the total number of occurrences using grep:

Example 1: Use “grep” with “wc(word count)” Utility

Execute the “grep” command with the combination “wc(word count)” utility with “-l(lines)” parameter to count the occurrence of “Linux” word from “SampleFile.txt”:

$ grep -o -i Linux SampleFile.txt | wc -l

The description of the command is defined here:

  • grep: Represents the “grep” command for searching.
  • -o: Counts the number of occurrences of the searched string/pattern in the entire file.
  • -i: Ignore the case sensitive i.e lower/uppercase while matching.
  • |(pipe character)”: Pipes the output of the “grep” and “wc” commands.
  • wc -l: Denotes the “word count” utility that searches for the number of lines:

The searched word “Linux” occurrence in a “SampleFile.txt” file is “5”.

Highlight the Searched Word

Execute the “grep” command without any flag to highlight the “Linux” occurrence in the “SampleFile.txt” file:

$ grep Linux SampleFile.txt

The “Linux” word in the “SampleFile.txt” has been highlighted.

Example 2: Use “grep” with “tr(translates)” Utility

Another approach for counting the total number of occurrences using the “grep” command with the “tr” utility is as follows.

The “tr” command takes the input from the “SampleFile.txt” file and transforms all whitespaces to the “newline” character. On the other hand, the “grep” command searches for the “Linux” word from each line of a “SampleFile.txt”:

$ tr '[:space:]' '[\n*]' < SampleFile.txt | grep -i -c Linux

The description of each parameter in the above command is written here:

  • tr: Represents the “translate” command that deletes/translates characters.
  • [:space:]: Denotes the whitespaces in a file.
  • [\n*]: Shows the “newline” character.
  • <: Input Redirection that redirects the input i.e SampleFile.txt to the “tr” command.
  • grep: Shows the “grep” command.
  • -i: Ignore the case sensitive i.e lower/uppercase while matching.
  • -c: Prints the line count that matches the pattern i.e “Linux”.

The searched word “Linux” occurrence in a “SampleFile.txt” file is “5”.

Conclusion

The Linux “grep” command is utilized with the “wc(word count)” and “tr(translates)” options to count the total number of occurrences of the word/pattern. The “wc” command returns the total count of the matched pattern from an entire file. Whereas the “tr” command transforms the white spaces with a newline to allow the “grep” utility to search the specified pattern from each line of the file.

This post has covered all possible ways to count the total number of occurrences using the “grep” command.