title: How do I count the number of occurrences of a string in an entire file?
body: <p>Is there an inbuilt command to do this or has anyone had any luck with a script that does it?</p>
  
  <p>I am looking to count the number of times a certain string (not word) appears in a file. This can include multiple occurrences per line so the count should count every occurrence not just count 1 for lines that have the string 2 or more times.</p>
  
  <p>For example, with this sample file:</p>
  
  <pre><code>blah(*)wasp( *)jkdjs(*)kdfks(l*)ffks(dl
  flksj(*)gjkd(*
  )jfhk(*)fj (*) ks)(*gfjk(*)
  </code></pre>
  
  <p>If I am looking to count the occurrences of the string <code>(*)</code> I would expect the count to be 6, i.e. 2 from the first line, 1 from the second line and 3 from the third line. Note how the one across lines 2-3 does not count because there is a LF character separating them.</p>
  
  <p><strong>Update</strong>: great responses so far! Can I ask that the script handle the conversion of <code>(*)</code> to <code>\(*\)</code>, etc? That way I could just pass any desired string as an input parameter without worrying about what conversion needs to be done to it so it appears in the correct format.</p>