I have 3 columns
a 03 w
a 10 x
a 01 y
b 20 w
b 01 x
c 02 w
c 10 y
c 12 z
Expected output is
a 10 x
b 20 w
c 12 z
i.e. i need to sort column 2 but without changing the order of column 1 then grep the line with max value in the list based on 2nd column
Two approaches (choose one you like):
1) sort + uniq "trick":
sort -k1,1 -k2,2rn file | uniq -w1
-k1,1
- sort lines by the 1st field on 1st phase
-k2,2rn
- sort lines by the 2nd field numerically in reversed order
uniq -w1
- output unique lines comparing no more than 1
character in lines (can be adjustable -w<number>
)
The output:
a 10 x
b 20 w
c 12 z
2) Simply with GNU datamash tool:
datamash -Wsf -g1 max 2 <file | cut -f1-3
The output:
a 10 x
b 20 w
c 12 z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With