awk to select lines with maximum value of each record based on column value

Hello,
I want to get the maximum value of each record separated by empty line based on the 3rd column of each row within each record?
Input:

A1    chr5D    634    7    82    707
A2    chr5D    637    6    82    713
A3    chr5D    637    5    82    713
A4    chr5D    626    1    82    704
A5    chr5D    723    0    1    723
A6    chr5D    734    12    1    722
A7    chr5D    719    3    1    715
                    
B5    chr6B    344    2    64    403
B6    chr6B    738    1    1    735
B7    chr6B    738    5    1    732
B8    chr6B    738    1    1    735
B9    chr6D    732    0    1    732
B10    chr6D    735    1    1    735
B11    chr6D    735    1    1    735
                    
C1    chr6D    735    0    1    735
C2    chr6D    735    2    1    732
C3    chr6D    735    0    1    735
                    
C4    chr7A    678    0    1    678
C5    chr7A    681    10    1    660
C6    chr7A    678    0    1    678
C7    chr7A    674    11    42    687

Output:

A6    chr5D    734    12    1    722
B8    chr6B    738    1    1    735
C3    chr6D    735    0    1    735
C5    chr7A    681    10    1    660

So far, I have tried:

awk 'BEGIN{FS="\n"; RS=""} $3 > max[NR] {maxline[NR]=$0} END{ for (i in maxline) {print maxline}}' input.tab 

but it did not give what I expected.
There are several issues with my script.
1) $3 is for each line, but not correct within each record;
2) same issue with $0;
It seems to me the trick is the FS value and maxline[NR]=$0 in my case.

Thanks a lot!

a bit of a long way...
awk -f yi.awk myFile where yi.awk is:

!NF {print maxline; max=0}
$3>=max{ max=$3;maxline=$0}
END { if (max) print maxline}
1 Like

How did you come with this trick "!NF" !

I am not sure if the END block get executed at each empty line. Could you please elaborate END block for me?
Thanks!

I guess by RTFMing? :wink:

---------- Post updated at 04:37 PM ---------- Previous update was at 04:23 PM ----------

there might be no trailing empty line after the LAST block (as in you example).
So the '!NF' will never catch the last record. So we're catching it in the END block - after ALL the records have been processed.

1 Like

Thanks! I get it now.