title: How can I get the hostname from a file using awk and regex or substring
body: <p>The file name is in a format like this: </p>
  
  <pre><code>YYYY-MM-DD_hostname_something.log
  </code></pre>
  
  <p>I want to get the hostname from the filename. The hostname can be any length, but always has a _ before and after. This is my current awk statement. It worked fine until the hostname length changed. Now I can't use it anymore.</p>
  
  <pre><code>awk 'BEGIN { OFS = "," } FNR == 1 { d = substr(FILENAME, 1, 10) } { h = substr(FILENAME, 12, 10) } $2 ~ /^[AP]M$/ &amp;&amp; $3 != "CPU" { print d, $1 "" $2, h, $4+$5, $6, $7+$8+$9}' *_something.log &gt; myfile.log
  </code></pre>