Selecting first nth rows by groups using AWK -


i have following file 4 fields. there 3 groups in field 2, , 4th field consists 0's , 1's.

the first field index.

i use awk following task

  1. select first 3 rows of group 1 (note group 1 has 2 rows). number of rows based on number of 1's found in 4th field times 3.

  2. select first 6 rows of group 2. number of rows based on number of 1's found in 4th field times 3.

  3. select first 9 rows of group 3. number of rows based on number of 1's found in 4th field times 3.

so 17 rows selected output file.

thank help.

input   1   1  tn1148 1 2   1  s52689 0 3   2  ta2081 1 4   2  ta2592 1 5   2  ta4011 0 6   2  ta4246 0 7   2  ta4275 0 8   2  tb0159 0 9   2  tb0392 0 10  3  tb0454 1 11  3  tb0496 1 12  3  tb1181 1 13  3  tc0027 0 14  3  tc1340 0 15  3  tc2247 0 16  3  tc3094 0 17  3  td0106 0 18  3  td1146 0 19  3  td1796 0 20  3  td3587 0  output    1  1  tn1148 1  2  1  s52689 0  3  2  ta2081 1  4  2  ta2592 1  5  2  ta4011 0  6  2  ta4246 0  7  2  ta4275 0  8  2  tb0159 0  10 3  tb0454 1  11 3  tb0496 1  12 3  tb1181 1  13 3  tc0027 0  14 3  tc1340 0  15 3  tc2247 0  16 3  tc3094 0  17 3  td0106 0  18 3  td1146 0 

the key awk program pass input file in twice: once count how many rows want , once print them.

awk '     nr == fnr {wanted_rows[$2] += 3*$4; next}      --wanted_rows[$2] >= 0 {print} ' input_file.txt input_file.txt 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -