title: how to use index of awk for loop in the regular expression
body: <p>I make the problem shorter. Actually I have data much longer than this.</p>
  
  <p>I have a file like:</p>
  
  <pre><code>aa, bb, cc, dd, ee, 4
  ff, gg, hh, ii, jj, 5
  kk, ll, mm, nn, oo, 3
  pp, qq, rr, ss, tt, 2
  uu, vv, ww, xx, yy, 5
  aa, bb, cc, dd, ee, 2
  </code></pre>
  
  <p>now I want to use awk to select each line with the same number in last column and redirect it into a new file, these new files will be different depending on the number in the last column.
  eg. t2.txt, t3.txt, t4.txt, t5.txt will hold the lines with last number as 2,3,4,5 respectively.</p>
  
  <p>in t2.txt:</p>
  
  <pre><code>pp, qq, rr, ss, tt, 2
  aa, bb, cc, dd, ee, 2
  </code></pre>
  
  <p>in t3.txt:</p>
  
  <pre><code>kk, ll, mm, nn, oo, 3
  </code></pre>
  
  <p>in t4.txt:</p>
  
  <pre><code>aa, bb, cc, dd, ee, 4
  </code></pre>
  
  <p>in t5.txt:</p>
  
  <pre><code>ff, gg, hh, ii, jj, 5
  uu, vv, ww, xx, yy, 5
  </code></pre>
  
  <p>I guess I need something like this:</p>
  
  <pre><code>BEGIN   {FS=","}
          {
          for (n=2; n&lt;=5; n++)
          if ($6 ~/\$n/) {print &gt; "t\$n.txt"}
          }       
  </code></pre>
  
  <p>But I just don't know how to make it work. </p>
  
  <p>This bash file do what I want, yet the problem is, each time it extracts lines with a specific number, it has to read in all the lines. How can I check ONLY TIME of the file and extract files for all numbers?</p>
  
  <pre><code>#!/bin/bash
  for num in {2..5}; do      
  gawk --assign FS="," "\$6 ~/${num}/" infile &gt;&gt; t${num}.txt
  done
  </code></pre>