1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Balash
  • 67
  • 4
  • 1
    Does this answer your question? [Sort CSV file by column priority using the "sort" command](https://stackoverflow.com/questions/9471101/sort-csv-file-by-column-priority-using-the-sort-command) – kvantour Nov 03 '19 at 10:48

1 Answers1

2

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
dash-o
  • 13,723
  • 1
  • 10
  • 37