How to count using the grep command in Linux/Unix

Linux grep command is one of the most commonly used command-line tools. We often use it to check the number of times of a words, phrases, strings in a text file or patterns to find the number of occurrences of files with specific names under folders.

Of course, you can also use a pipeline to do your daily work using a combination of grep and other commands. For example, use the grep command and the wc command to count the number of files that contain the specified content.

Suppose you have an test6.txt file containing the sentences:

Grep counts the number of lines in the file that contain the specified content

In the following example, we will use the grep command to count the number of lines in the file test6.txt that contain the string “dfff”

➜  grep -c "dfff" test6.txt

Using grep -c options alone will count the number of lines that contain the matching word instead of the number of total matches.

You can also use the grep command, pipe, and wc command to achieve the same effect as the grep-c option in the following example.

➜  grep "dfff" test6.txt | wc -l

Grep counts the number of times of the specified content in a file

In the following example, we use grep -w to count the number of times of the string “dfff” in the file

➜  grep -o -w  "dfff" test6.txt | wc -l
Options:
-o, --only-matching          
     Prints only the matching part of the lines.  
-w, --word-regexp          
     The expression is searched for as a word (as if surrounded by `[[:<:]]' and `[[:>:]]'; see re_format(7)).

Grep count the number of files in the directory whose filename contains the specified keyword

In the following example, the grep directory contains files whose filenames contain the keyword “test”, and we use the ls command, pipe, and wc command to count the number of files whose filenames contain the keyword “test” in the directory.

➜ ll | grep -c test

OR
➜ ll | grep test | wc -l

In the example above, we can count the number of lines or the total number of occurrences of a keyword in a file.


Sometimes, however, we also need to count the keyword to appear in the file, at the same time, according to the line number in reverse order.

Grep matches and reverses by line number

➜ grep -n -w "dfff" test6.txt | sort -r

➜ grep -w "dfff" test6.txt | sort

grep two words on the same line

Grep matches multiple keywords, which we often use on a daily basis. But matching multiple keywords has two meanings:
* Match file containing keyword1 and containing keyword2 … : AND
* Match file containing keyword1 or containing keyword2 … : OR

In the first example, we use the grep -e option to match the line containing the word “dfff” or “apple” in the file test6.txt.

➜  grep -n -w -e "dfff" -e "apple" test6.txt


In the second example, we used multiple grep commands and pipes to match lines containing both “dfff” and “apple” words in the file test6.txt.

➜  grep -n -w "dfff" test6.txt | grep apple

Grep count the number of non-duplicate lines in a file

Use the grep command to count the number of the same lines in the file?

sorry, I do not know . . .

But it can be like this:

➜ cat test7.txt| sort| uniq | wc -l

Add a Comment

Your email address will not be published. Required fields are marked *