Count number of occurrences of a pattern in a file (even on same line)
Posted
by jrdioko
on Stack Overflow
See other posts from Stack Overflow
or by jrdioko
Published on 2010-05-25T21:44:51Z
Indexed on
2010/05/25
22:01 UTC
Read the original article
Hit count: 225
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?
© Stack Overflow or respective owner