title: awk: extract data from a column by name rather than position
body: <p>I have a text file that is comma delimited. The first line is a list of field names, and subsequent lines contain data. I'll get new versions of the file, and I want to extract all the values from a particular column by name rather than by column number.  (I.e. the column I want may be in different positions in different versions of the file.)</p>
  
  <p>For example, here are two files:</p>
  
  <pre><code>foo,bar,interesting,junk
  1,2,gold,ramjet
  2,25,diamonds,superfluous
  </code></pre>
  
  <p>and</p>
  
  <pre><code>foo,bar,baz,interesting,junk,morejunk
  5,3,smurf,platinum,garbage,scrap
  6,2.5,mushroom,sodium,liverwurst,eew
  </code></pre>
  
  <p>I'd like a single script that will go through multiple files, extracting the minerals in the "interesting" column. :-)</p>
  
  <p>What I've got so far is something that works on ONE file, but I know that awk is more elegant than this. How do I clean this up and make it work on multiple files at once?</p>
  
  <pre><code>BEGIN {
      FS=",";
  }
  
  NR == 1 {
      for(i=1; i&lt;=NF; i++) {
          if($i=="interesting") {
              col=i;
          }
      }
  }
  
  NR &gt; 1 {
    print $col;
  }
  </code></pre>