title: Extract date from filename using bash script
body: <p>I know that similar things have been asked before, but I haven't been able to really make hand and foot out of what's been posted.</p>
  
  <p>I've got a whole bunch of files that contain the date in the format YYYYMMDD at some point in the filename. Luckily this is the only 8 digit substring in all the filenames!</p>
  
  <p>I will need to write the dates into another file later, but that should be fine. I'm struggling to extract the date into a variable first...</p>
  
  <p>I know I can get it with grep:</p>
  
  <pre><code>for d in $( ls *.csv | grep -Po "\d{8}"; do 
  echo $d done
  </code></pre>
  
  <p>However, as I want to get the full filename into a variable too while I iterate through them, that's not an option right now.</p>
  
  <p>I've tried using sed, but I don't think I know how to use it:</p>
  
  <pre><code>for f in $( ls *.csv ); do
      d=$( $f | sed -e 's/^.*\(\d{8}\).*$')
      echo $d
  done
  </code></pre>
  
  <p>Thanks for pointing me in the right direction!</p>