# Find the maximum every 10 rows in a particular column and print the entire row

I would like to find the maximum value in the column no. 5 (Determine) for every 10 rows and then print the corresponding entire row. For eg. in the first 10 rows, the maximum value in the 5th column is '1.1824' and the corresponding entire row is "10_M62 6: 3.0561 405.69 1.1824". I want to find the maximum value in the 5th column for every 10 rows and then print the entire column like show above.

I tried the following command, it works for first 30 rows after which it gives wrong answers. I am not sure what I am missing. Kindly help me.

``````awk 'm<\$10{m=\$10;n=\$5" "\$2\$3 \$4" "\$6" "\$7" "\$8" "\$9} !((NR+2)%10){print \$1,n,m;m=n=""}'
``````

Please let me know if I am not clear. I have given the sample list below.

``````Name      No.           Value1   Value2   Determine

10_M62    1:            2.4578   504.44   0.0013
10_M62    2:            2.6155   474.03   0.0010
10_M62    3:            2.8581   433.80   0.0418
10_M62    4:            2.9552   419.54   0.3863
10_M62    5:            2.9809   415.93   0.1014
10_M62    6:            3.0561   405.69   1.1824
10_M62    7:            3.3083   374.77   0.0719
10_M62    8:            3.5159   352.63   0.0242
10_M62    9:            3.6366   340.94   0.0023
10_M62   10:            3.6553   339.19   0.0049
11_M63    1:            1.9677   630.10   0.0075
11_M63    2:            2.3544   526.62   1.8099
11_M63    3:            2.7363   453.12   0.0028
11_M63    4:            2.7437   451.88   0.0044
11_M63    5:            2.8913   428.81   0.0569
11_M63    6:            2.9497   420.32   0.3310
11_M63    7:            2.9688   417.63   0.0889
11_M63    8:            3.0038   412.76   0.0330
11_M63    9:            3.0792   402.65   0.2730
11_M63   10:            3.0805   402.48   0.0914
...
``````

This will work using any awk in any shell on every UNIX box even if your input values are all negative and/or your input is not an exact multiple of 10 lines and no matter how many blank lines are between your header line and your data:

``````\$ cat tst.awk
NR==1 { print; next }
!NF { next }
(++numLines) % 10 == 1 {
printf "%s", maxLine
maxVal = \$5
maxLine = ""
}
\$5 >= maxVal {
maxVal = \$5
maxLine = \$0 ORS
}
END { printf "%s", maxLine }
``````

.

``````\$ awk -f tst.awk file
Name      No.           Value1   Value2   Determine
10_M62    6:            3.0561   405.69   1.1824
11_M63    2:            2.3544   526.62   1.8099
``````
• Thank you very much. It worked. I sincerely appreciate your effort.
– Rag
Commented Aug 12, 2020 at 15:06
• You're welcome. See stackoverflow.com/help/someone-answers for what to do next. Commented Aug 12, 2020 at 17:14