Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of occurrences of a pattern in a file (even on same line)

When searching for number of occurrences of a string in a file, I generally use:

grep pattern file | wc -l 

However, this only finds one occurrence per line, because of the way grep works. How can I search for the number of times a string appears in a file, regardless of whether they are on the same or different lines?

Also, what if I'm searching for a regex pattern, not a simple string? How can I count those, or, even better, print each match on a new line?

like image 534
jrdioko Avatar asked May 25 '10 21:05

jrdioko


People also ask

How do you count the number of occurrences of a file in a pattern?

Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.

How do you find the number of occurrences in a string?

count() One of the built-in ways in which you can use Python to count the number of occurrences in a string is using the built-in string . count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.


2 Answers

To count all occurrences, use -o. Try this:

echo afoobarfoobar | grep -o foo | wc -l 

And man grep of course (:

Update

Some suggest to use just grep -co foo instead of grep -o foo | wc -l.

Don't.

This shortcut won't work in all cases. Man page says:

-c print a count of matching lines 

Difference in these approaches is illustrated below:

1.

$ echo afoobarfoobar | grep -oc foo 1 

As soon as the match is found in the line (a{foo}barfoobar) the searching stops. Only one line was checked and it matched, so the output is 1. Actually -o is ignored here and you could just use grep -c instead.

2.

$ echo afoobarfoobar | grep -o foo foo foo  $ echo afoobarfoobar | grep -o foo | wc -l 2 

Two matches are found in the line (a{foo}bar{foo}bar) because we explicitly asked to find every occurrence (-o). Every occurence is printed on a separate line, and wc -l just counts the number of lines in the output.

like image 187
hudolejev Avatar answered Oct 02 '22 14:10

hudolejev


Try this:

grep "string to search for" FileNameToSearch | cut -d ":" -f 4 | sort -n | uniq -c 

Sample:

grep "SMTP connect from unknown" maillog | cut -d ":" -f 4 | sort -n | uniq -c   6  SMTP connect from unknown [188.190.118.90]  54  SMTP connect from unknown [62.193.131.114]   3  SMTP connect from unknown [91.222.51.253] 
like image 42
IBrewThereforeIAm Avatar answered Oct 02 '22 13:10

IBrewThereforeIAm