title: Bash retrieve column number from column name
body: <p>Is there a better way (such as a one liner in AWK) where I can get the column number in a table with headings from a column name?  I want to be able to process a column independent of what the column number actually is (such as when another column is added the script will not need to change).</p>
  
  <p>For example, given the following table in "table.tsv":</p>
  
  <pre><code>ID  Value   Target  Not Used
  1   5   9   11
  2   4   8   12
  3   6   7   10
  </code></pre>
  
  <p>I can do a sort on the "Target" column using:</p>
  
  <pre><code>#!/bin/bash
  (IFS=$'\t'; read -r; printf "%s\n" "$REPLY"; i=0; for col in $REPLY; do
      ((++i))
      [ "$col" == "Target" ] &amp;&amp; break
  done; sort -t$'\t' "-k$i,${i}n") &lt; table.tsv
  </code></pre>
  
  <p>Is there a way to do it without the for loop (or at least clean it up a little)?</p>
  
  <p>The expected output of the given script is:</p>
  
  <pre><code>ID      Value   Target  Not Used
  3       6       7       10
  2       4       8       12
  1       5       9       11
  </code></pre>
  
  <p>However, I was trying to give an example of what I was trying to do.  I want to pass/filter my table through several programs so the headings and all columns should be preserved:  just have processing occur at each step.
  In pseudo code, what I would like to do is:</p>
  
  <pre><code>print headings from stdin
  i=$(magic to determine column position given "Target")
  sort -t$'\t' "-k$i,${i}n"  # or whatever processing is required on that column
  </code></pre>