title: Bash; check if certain lines contain characters
body: <pre><code>if grep -q "_" "${changefile}"; then
    fail
    printf "\nUnderscores are invalid in hostnames\n" &gt;&amp;2
    exit 1
  fi
  </code></pre>
  
  <p>I have this snippet in place as a method to check a DNS configuration file if it contains an underscore; an invalid character. If so, it will crash. </p>
  
  <p>I need to rewrite this so it checks if any of the hostnames contain invalid characters. The hostnames are on lines 27-350. </p>
  
  <p>My idea's logic revolves around: <em>"if grep -q "${changefile}" lines 27 through 350 returns any invalid characters; complain and exit"</em>. Just not sure how to code that.</p>
  
  <p>Quoting Microsoft's website, a hostname must satisfy the fact that characters only:</p>
  
  <blockquote>
    <p>in the range of 'A-Z', 'a-z','0-9', '.' and '-' are used.</p>
  </blockquote>