title: Count number of occurrences of a pattern in a file (even on same line)
body: <p>When searching for number of occurrences of a string in a file, I generally use:</p>
  
  <pre><code>grep pattern file | wc -l
  </code></pre>
  
  <p>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?</p>
  
  <p>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?</p>