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?