firoz
firoz

Reputation: 91

selecting highest value in a table

A file having 3 columns with tab separated. I want to select the highest value from column 3rd (among the same name of column 1st) and print as the 4th column.

input file

A       l       10
A       l       2
A       m       6
B       l       12
B       m       13
B       n       7
C       l       9
C       l       8
C       n       19

Output file

A       l       10      10
A       l       2       10
A       m       6       10
B       l       12      13
B       m       13      13
B       n       7       13
C       l       9       19
C       l       8       19
C       n       19      19

Could you please suggest awk,or sed command. Thanks

Upvotes: 0

Views: 80

Answers (1)

Jotne
Jotne

Reputation: 41456

You can use this awk

awk 'FNR==NR {arr[$1]=arr[$1]>$3?arr[$1]:$3;next} {print $0,arr[$1]}' OFS="\t" file{,}
A       l       10      10
A       l       2       10
A       m       6       10
B       l       12      13
B       m       13      13
B       n       7       13
C       l       9       19
C       l       8       19
C       n       19      19

This passes two times over the file. First time to find highest, next to print.
The file{,} make the filename double. You can also use file file instead.

Upvotes: 1

Related Questions