title: dividing a data file to new files based on data on a particular column
body: <p>I have a data file (data.txt) as shown below:</p>
  
  <pre><code>0  25  10  25000
  1  25  7   18000
  1  25  9   15000
  
  0  20  9   1000
  1  20  8   800
  0  20  8   900
  
  0  50  10  4000
  0  50  5   2500
  1  50  10  5000
  </code></pre>
  
  <p>I want to copy the rows with same value in the second column to separate files. I want to get following three files:</p>
  
  <p>data.txt_25</p>
  
  <pre><code>0  25  10  25000
  1  25  7   18000
  1  25  9   15000
  </code></pre>
  
  <p>data.txt_20</p>
  
  <pre><code>0  20  9   1000
  1  20  8   800
  0  20  8   900
  </code></pre>
  
  <p>data.txt_50</p>
  
  <pre><code>0  50  10  4000
  0  50  5   2500
  1  50  10  5000
  </code></pre>
  
  <p>I have just started learning awk. I have tried the following bash script:</p>
  
  <pre><code>  1 #!/bin/bash
    2 
    3 for var in 20 25 50
    4 do
    5         awk -v var="$var" '$2==var { print $0 }' data.txt &gt; data.txt_$var
    6 done
  </code></pre>
  
  <p>While the bash script does what I want it to do, it is time consuming as I have to put the values of second column data in line 3 manually.  </p>
  
  <p>So I would like to do this using awk. How can I achieve this using awk ?</p>
  
  <p>Thanks in advance.</p>