How To Count All Matches of a String With grep For Linux

grep is a textual content search utility that may work with normal enter or a number of information directly. It’s used to print out matches for patterns, strings, or common expressions. It’s usually helpful to have the ability to depend the variety of matches, which grep can do fairly simply.

Counting Matches With grep

The grep command has the -c flag, which is able to depend the variety of strains matched and print out a quantity. That is helpful for plenty of issues, equivalent to looking out by way of log information for the variety of entries from a particle IP, endpoint, or different identifier.

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

Nonetheless, grep is ready to match a number of instances per line. For those who use the -o flag, grep will print out a brand new line per match. This doesn’t work with the -c flag, as it can solely depend matching strains, not a number of matches per line.

A greater answer is to make use of the wc (phrase depend) utility with the -l (strains) parameter, which is able to depend the uncooked variety of strains handed to it over normal enter. Utilizing wc -l is the popular answer as a result of it really works with -o to depend the variety of occurrences of the given string or sample throughout all the file.

grep -o "foo" file | wc -l

Counting Throughout A number of Information

A pleasant characteristic of grep is the flexibility to deal with a number of information directly, both handed by way of xargs, parameters, or provided with wildcard growth. When dealing with a number of information, grep will print out the filename earlier than the match. When utilizing -c to depend the variety of matching strains, it can additionally print out the filenames:

grep "foo" ./*.txt -cH

It’s best to at all times use the -H flag when working with the potential for a number of information, since it can at all times print the filename even when there’s just one file handed to grep. This can stop automation from breaking should you rely on the filename being there.

If you wish to use -o to depend a number of matches per line, and move the output to wc -l, you’ll sadly the flexibility to see the numbers for every particular person file like with -c. Nonetheless, with a little bit of scripting, you may chop the primary column off with lower, and depend the variety of distinctive occurrences for every filename with uniq -c:

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

RELATED: Methods to Extract and Kind Columns Out of Log Information on Linux

Source link

Leave a Comment

Your email address will not be published.