Balash
Balash

Reputation: 67

How to sorting multiple columns from a CSV file

I have a file in following format. I need to sort it by largest code first and then by largest values column.

 colour,letter,code,value
    red,r,016,949.8
    red,r,015,603.9
    red,r,014,348.4
    blue,b,016,362.29
    blue,b,015,460.2
    blue,b,014,9850.9

output:
    red,r,016,949.8
    blue,b,016,362.29
    red,r,015,603.9
    blue,b,015,460.2
    blue,b,014,9850.9
    red,r,014,348.4

my implementation

sort  -k3,3n -r  -k4,4n -t \t data.csv  

When I try to do it it sorts the file but doesn't sort the first two columns.

Upvotes: 1

Views: 589

Answers (1)

dash-o
dash-o

Reputation: 14491

It's not clear if the file is TSV (tab separated), or CSV (comma separated). Question indicate CSV, but answer using tab delimiter (-t \t). Try -t, for CSV. Also the reverse order need to be applied to each key ('r' suffix on each key).

sort -k3,3nr -k4,4nr -t, data.csv

Upvotes: 2

Related Questions