How to count all matches of a string with grep for Linux

0
29


grep is a text search utility that can work with standard input or multiple files at once. Used to print pattern, string, or regular expression matches. It is often useful to be able to count the number of matches, which grep can do quite easily.

Counting hits with grep

the grep The command has -c flag, which will count the number of matching lines and print a number. This is useful for many things, such as searching log files for the number of entries for a particle IP, endpoint, or other identifier.

grep -c "1.2.3.4" /var/log/nginx/access.log

Nevertheless, grep is capable of matching multiple times per line. If you use the -o flag, grep will print a new line by match. This doesn’t work with -c mark, as it will only count matches linesnot multiple matches per line.

A better solution is to use the wc (word count) utility with the -l (lines) parameter, which will count the raw number of lines passed to it over standard input. Wearing wc -l is the preferred solution because it works with -o to count the number of occurrences of the given string or pattern in the entire file.

grep -o "foo" file | wc -l

Counting across multiple files

A nice feature of grep is the ability to handle multiple files at once, either past xargs, parameters, or provided with wildcard expansion. When handling multiple files, grep will print the file name before the match. when you use -c to count the number of matching lines, it will also print the filenames:

grep "foo" ./*.txt -cH

You should always use the -H check when working with the multiple files possibility, as it will always print the file name even if only one file is passed to grep. This will prevent the automation from breaking if it depends on the filename being there.

if you want to use -o to count multiple matches per line and pass the output to wc -lunfortunately you will be able to see the numbers of each individual file as with -c. However, with a bit of scripting, you can chop off the first column with cutand count the number of unique occurrences for each filename with uniq -c:

grep "foo" ./*.txt -o | cut -d ':' -f 1 | uniq -c

RELATED: How to extract and sort columns from log files in Linux