I'm trying to sort 3 columns numerically in reverse (descending) by the third column first and then sort by the first column alphabetically (to break ties). Entries are delimited by commas( ,
).
For example, my dataset is:
y,5,50
x,10,50
z,4,100
Expected output:
z,4,100
x,10,50
y,5,50
However the output I am getting is:
z,4,100
y,5,50
x,10,50
I am using:
sort -t, -k3,3 -n -r -k1,1 filename
Not sure why this is not working.
I suggest to replace -k3,3 -n -r
by -k3,3nr
:
sort -t, -k3,3nr -k1,1 file
Output:
z,4,100 x,10,50 y,5,50
The reason what you're suggesting doesn't work is becuase you've applied the flags -n
, -r
globally hence the alphabetical sorting is also -r
eversed. To apply the flag on a per key basis, use:
sort -t, -k3,3nr -k1,1 filename
This gives the expected output:
z,4,100
x,10,50
y,5,50
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.