title: Bash: Sort file numerically, but only where the first field matches a pattern
body: <p>Due to poor past naming practices, I'm left with a list of names that is proving to be a challenge to work with. The bottom line is that I want the most current name (by date) to be placed in a variable. All the names are listed (unsorted) in a file called <code>bar.txt</code>.</p>
  <p>In this case I can't rename, and there's no way to get the actual dates of the images; these names are all I have to go on. The names can follow one of several patterns;</p>
  <pre><code>foo
  YYYYMMDD-foo
  YYYYMMDD##-foo
  </code></pre>
  <p><code>foo</code> can be anything from a single character to a long string of letters/numbers/symbols. I am interested only in the names matching the second use case, <code>YYMMDD-foo</code>, as those are from after we started tagging consistently.</p>
  <p>I would like to end up with a variable containing the most recent date that follows the pattern <code>YYMMDD-foo</code>.</p>
  <p>I know <code>sort -k1 -n &lt; bar.txt</code>, but then I'm not sure how to isolate the second pattern's results to extract what I need.</p>
  <p><strong>How do I sort the file to ignore anything but the second pattern, and return the most current date?</strong></p>
  <h2>Sample</h2>
  <p>Given that bar.txt looks like this;</p>
  <pre><code>test
  2017120901-develop-BUILD-31
  20170326-TEST-1.2.0
  20170406-BUILD-40-1.2.0-test
  2010818_001
  </code></pre>
  <p>I would want to extract <code>20170406-BUILD-40-1.2.0-test</code></p>