linux grep count lines/words, count sort

When we use linux grep to find matching content, we often need to count the number of matching content.

Let me introduce you to several statistical scenarios.

grep count lines

Match the number of lines containing the string “dfff” in the file test6.txt

➜  grep -c "dfff" test6.txt

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

grep count words

Match the number of lines containing the string “dfff” in the file test6.txt

➜  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 files

➜ ll | grep -c test

OR
➜ ll | grep test | wc -l

grep count sort

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

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

grep two words on the same line

Matches lines that appear with the strings “dfff” and “apple” on the same line.

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

grep count same line

The number of identical lines in the statistics file.
Using linux grep?

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 *