title: "grep -c" versus "wc -l"
body: <p>I am processing a number of large text files, ie. converting them all from one format to another. There are some small differences in the original formats of the files, but - with a bit of pre-processing in a few cases - they are mostly being successfully converted with a bash shellscript I have created.</p>
  
  <p>So far so good, but one thing is puzzling me. At one point the script sets a variable called <code>$iterations</code>, so that it knows how many times to perform a particular for-loop. This value is determined by the number of empty lines in a temporary file that is created by the script.</p>
  
  <p>Thus, the original version of my script contained the line:</p>
  
  <pre><code>    iterations=$(cat tempfile | grep '^$' | wc -l)
  </code></pre>
  
  <p>This has worked fine so far with all but one of the text files, which didn't seem to set the <code>$iterations</code> variable correctly, giving a value of '1' even though there appeared to be more than 20,000 empty lines in <code>tempfile</code>.</p>
  
  <p>However, having discovered <code>grep -c</code>, I changed the line to:</p>
  
  <pre><code>    iterations=$(cat tempfile | grep -c '^$')
  </code></pre>
  
  <p>and the script suddenly worked, ie. <code>$iterations</code> was set correctly.</p>
  
  <p>Can anyone explain why the two versions produce different results? And why the first version would work on some files and not others? Is there some upper limit value above which <code>wc -l</code> defaults to 1? The file which wouldn't work with the first version is one of the largest, but not <em>the</em> largest in the set (which converted correctly the first time).</p>