title: Regex match entire contents of file in bash
body: <p>What is the best way to test if the complete contents of a file matches a regex, such as</p>
  
  <pre><code>^[0-9]{9}$
  </code></pre>
  
  <p>i.e., just 9 numbers and nothing else, no line breaks, and not multiple sets of numbers.</p>
  
  <p>Here is one variant I have that I do not really like:</p>
  
  <pre><code>cat -vt curloutput.txt | tr "\n" " " | egrep "^[0-9]{9}$"
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>I use the accepted solution like this:</p>
  
  <pre><code>grep --perl-regex "(?m)(?&lt;!.)^\d{9}$(?!.)"
  </code></pre>
  
  <p>using GNU grep.</p>