title: Move column to start by header keyword in bash
body: <p>I have a file (data.rdb) with the following format:</p>
  
  <pre><code>date    star    jdb texp
  2013-11-22  epsInd      2400000.23551544    100.
  2013-11-22  epsInd      2400000.23551544    100.
  2013-11-22  epsInd      2400000.23551544    100.
  2013-11-22  HD217987    2400000.23551544    900.
  2013-11-22  TOI-134     2400000.23551544    900.
  2013-11-22  tauCet      2400000.23551544    60. 
  2013-11-22  BD+01316    2400000.23551544    300.
  2013-11-22  BD+01316    2400000.23551544    300.
  2013-11-22  BD+01316    2400000.23551544    300.
  2013-11-22  BD+01316    2400000.23551544    300.
  </code></pre>
  
  <p>some properties:</p>
  
  <ul>
  <li>all columns are tab separated</li>
  <li>the columns do not have the same width</li>
  <li>the cells might not have the same length</li>
  <li>the file will have much more columns than presented and a few hundreds of  lines</li>
  <li>the columns names can be any word, with no tabs or spaces or special characters</li>
  </ul>
  
  <p>How can I move the column with header <code>jdb</code> to be the first column?</p>
  
  <p>Some constrains:</p>
  
  <ul>
  <li>this will be applied to multiple files, and the column <code>jdb</code> will not always appear at the same position</li>
  <li>ideally the order of the remaining columns should not change</li>
  <li><code>jdb</code> will always be the 1st column in the end.</li>
  </ul>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>this is the <code>awk</code> block I am using at the moment:</p>
  
  <pre><code>BEGIN {
      numCols = split(column_list,cols)
      OFS="\t"
  }
  { sub(/\r$/,"") }
  NR==1 {
      for (fldNr=1; fldNr&lt;=NF; fldNr++) {
          f[$fldNr] = fldNr
      }
  }
  {
      for (colNr=1; colNr&lt;=numCols; colNr++) {
          colName = cols[colNr]
          colVal  = (colNr=1 ? $(f["jdb"]): (colNr &lt;= $(f["jdb"] ? 
  $(f[colName] -1) : $(f[colName]))))
          printf "%s%s", colVal, (colNr&lt;numCols ? OFS : ORS)
      }
  }
  </code></pre>
  
  <p>but it gives me no output... What I (think I) did:</p>
  
  <ol>
  <li><p>assign each column header value a number</p></li>
  <li><p>iterate over a range</p>
  
  <p>2.1 if iterator = 0 -> print column <code>jdb</code></p>
  
  <p>2.2 if iterator &lt;= column number of jdb -> print column number <code>iterator - 1</code></p>
  
  <p>2.3 if iterator >  column number of jdb -> print column number <code>iterator</code></p></li>
  </ol>
  
  <p><em>(this is on the continuation of the question I posed in <a href="https://stackoverflow.com/questions/56132249/extract-columns-from-tab-separated-file)_">https://stackoverflow.com/questions/56132249/extract-columns-from-tab-separated-file)</em></a></p>
  
  <hr>
  
  <p><strong>END RESULT</strong></p>
  
  <p>In the end I ended up using @Ed Morton's solution:</p>
  
  <pre><code>$ cat move_to_first.awk
  BEGIN { FS=OFS="\t" }
  NR==1 {
      cols[++numCols] = tgt
      for (fldNr=1; fldNr&lt;=NF; fldNr++) {
          f[$fldNr] = fldNr
          if ($fldNr != tgt) {
              cols[++numCols] = $fldNr
          }
      }
  }
  {
      for (colNr=1; colNr&lt;=numCols; colNr++) {
          colName = cols[colNr]
          printf "%s%s", $(f[colName]), (colNr&lt;numCols ? OFS : ORS)
      }
  }
  </code></pre>
  
  <p>As a curiosity, to move the column to the last position, the above code just needs the following modification:</p>
  
  <pre><code>$ cat move_to_last.awk
  BEGIN { 
      FS=OFS="\t" 
      }
  NR==1 {
      for (fldNr=1; fldNr&lt;=NF; fldNr++) {
          f[$fldNr] = fldNr
          if ($fldNr != target) {
              cols[++numCols] = $fldNr
          }
      }
      cols[++numCols] = target
  }
  {
      for (colNr=1; colNr&lt;=numCols; colNr++) {
          colName = cols[colNr]
          printf "%s%s", $(f[colName]), (colNr&lt;numCols ? OFS : ORS)
      }
  }
  </code></pre>