Oct 20, 2023 3 min read

How to Use Grep to Calculate the Total Number of Occurrences?

In this tutorial, you will learn how to count the total number of occurrences using the “grep” command.

Use Grep to Calculate the Total Number of Occurrences
Table of Contents

Introduction

Before we begin talking about how to use grep to calculate the total number of occurrences, let's briefly understand – What is grep?

The "grep" command runs a file search on the given word, string, or pattern of characters. This application searches the specific pattern or word from the file by default and presents the results in the terminal. However, it does not show the entire number of occurrences in digits of the specified word/pattern. The built-in command line tools "tr (translates)" and "wc (word count)" are used by "grep" for this process.

In this tutorial, you will use grep to calculate the total number of occurrences. We will also address a few FAQs on how to use grep to calculate the total number of occurrences.

How to Use Grep to Count the Total Number of Occurrences?

With the aid of real-world examples, the "grep" command line tool can count the total number of occurrences using a wide range of supported parameters.

Sample File

As an example, the following text is displayed in the terminal for the file "SampleFile.txt":

cat SampleFile.txt

Let's do a practical grep implementation to count the total number of occurrences:

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

Use the "grep" command and the "wc (word count)" and "-l (lines)" parameters to count the number of times the term "Linux" appears in "SampleFile.txt":

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

Here is an explanation of the command's description:

  • grep: The "grep" command for searching.
  • -o: Counts the number of times the searched string or pattern appears throughout the whole file.
  • -i: When matching, ignore whether a word is lowercase or uppercase.
  • "|(pipe character)": Used to pipe "grep" and "wc" commands' output.
  • wc -l: Stands for the "word count" tool, which searches up the number of lines:

The number of times the term "Linux" appears in a file named "SampleFile.txt" is "5".

Highlight the Searched Word

To highlight the "Linux" occurrence in the "SampleFile.txt" file, use "grep" without any flags:

grep Linux SampleFile.txt

The term "Linux" has been highlighted in the "SampleFile.txt" file.

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

The following method uses the "grep" command and the "tr" utility to count the total number of occurrences.

The "tr" command converts all whitespace to the "newline" character using the input from the "SampleFile.txt" file. However, the "grep" tool looks for the word "Linux" in every line of a "SampleFile.txt":

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

Here is a description of each of the parameters in the aforementioned command:

  • tr: Stands in for the "translate" command, which deletes and/or translates characters.
  • [:space:]: Identifies whitespaces in a file.
  • [\n*]: Displays the "newline" character.
  • <: Input Redirection, which sends SampleFile.txt as input to the "tr" command.
  • grep: Displays the "grep" command.
  • -i: When matching, ignore whether a word is lowercase or uppercase.
  • -c: Displays the number of lines that contain the specified pattern, in this case, "Linux".

The number of times the term "Linux" appears in a file named "SampleFile.txt" is "5".

FAQs to Use Grep to Calculate the Total Number of Occurrences

How do I count case-insensitive occurrences using grep?

Use the -i option to make the search case-insensitive:-grep -ci "pattern" file.txt

Can I count occurrences of a pattern in a directory and its subdirectories?

Yes, you can use the -r option for recursive search:-grep -r -c "pattern" /path/to/directory

How can I count lines that match a pattern rather than occurrences?

Use the -l option instead of -c to count matching lines:-grep -l "pattern" file.txt

What if I want to count lines that do not match a pattern?

Use the -L option to count lines that don't match:-grep -L "pattern" file.txt

How can I count occurrences and display the matching lines as well?

Use the -n option to show line numbers with -c:-grep -nc "pattern" file.txt 

Is it possible to count the total occurrences of multiple patterns in a file?

Yes, you can use a pipe (|) to combine multiple grep commands:-(grep -c "pattern1" file.txt; grep -c "pattern2" file.txt) | awk '{s+=$1} END {print s}'

How do I count occurrences of multiple patterns using grep?

You can use the -e option to specify multiple patterns:-grep -c -e "pattern1" -e "pattern2" file.txt

Conclusion

The "wc (word count)" and "tr (translates)" options of the Linux "grep" command are used to count all instances of the word/pattern. In an entire file, the "wc" command delivers the overall count of the matched pattern. The "grep" command may search the supplied pattern from each line of the file by using the "tr" command, which replaces white spaces with newlines.

You now know the different ways to count the total number of occurrences using the “grep” command. If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Blog - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.