title: print columns based on column name
body: <p>Let's say I have a file <code>test.txt</code> that contains</p>
  <pre><code>a,b,c,d,e
  1,2,3,4,5
  6,7,8,9,10
  </code></pre>
  <p>I want to print out columns based on matching column names, either from another text file or from an array. So for example if I was given</p>
  <pre><code>arr=(a b c)
  </code></pre>
  <p>I want my output to then be</p>
  <pre><code>a,b,c
  1,2,3
  6,7,8
  </code></pre>
  <p>How can I do this with bash utilities/awk/sed? My actual text file is 3GB (and the line I want to match column values on is actually line 3), so efficient solutions are appreciated. This is what I have so far:</p>
  <pre><code>for j in &quot;${arr[@]}&quot;; do awk -F ',' -v a=$j '{ for(i=1;i&lt;=NF;i++) {if($i==a) {print $i}}}' test.txt; done
  </code></pre>
  <p>but the output I get is</p>
  <pre><code>a
  b
  c
  </code></pre>
  <p>which not only is missing the other rows, but each column name is printed on one line each.</p>