Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the columns with below requirement

Tags:

linux

vim

sed

awk

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

like image 303
Vinay V Avatar asked Mar 09 '23 03:03

Vinay V


1 Answers

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
like image 73
RomanPerekhrest Avatar answered Mar 16 '23 00:03

RomanPerekhrest