1

Source:

10,10,7.17,1.077383,0.00428382
10,12,7.45,1.177068,0.00390197
10,4,6.86,1.184806,0.00489828
10,6,6.98,1.106846,0.00463645
10,8,7.09,1.106254,0.00451672
12,10,6.71,1.224453,0.00506310
12,12,6.96,1.141856,0.00446641
12,4,6.41,1.510563,0.00590838
12,6,6.51,1.187841,0.00548915
12,8,6.62,1.217152,0.00532222

Desired result

10,4,6.86,1.184806,0.00489828
10,6,6.98,1.106846,0.00463645
10,8,7.09,1.106254,0.00451672
10,10,7.17,1.077383,0.00428382
10,12,7.45,1.177068,0.00390197
12,4,6.41,1.510563,0.00590838
12,6,6.51,1.187841,0.00548915
12,8,6.62,1.217152,0.00532222
12,10,6.71,1.224453,0.00506310
12,12,6.96,1.141856,0.00446641

How do i sort the csv for the first two column such i get the desired result in ascending order.

10,4
10,6
10,8
10,12

sort -k1,2 -n -t, didn't work as expected

10,4,6.86,1.184806,0.00489828
10,6,6.98,1.106846,0.00463645
10,8,7.09,1.106254,0.00451672
12,4,6.41,1.510563,0.00590838
12,6,6.51,1.187841,0.00548915
12,8,6.62,1.217152,0.00532222

You can see that 10,10,7.17,1.077383,0.00428382 is missing

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • I’m voting to close this question because it belongs on Unix.SE, and is also a dupe of https://stackoverflow.com/questions/9471101/sort-csv-file-by-multiple-columns-using-the-sort-command – Dan Dascalescu Apr 24 '22 at 20:48

2 Answers2

0

sort -k1,1 -k2,2 -n -t, worked fine

More info : https://unix.stackexchange.com/questions/78925/how-to-sort-by-multiple-columns

user2650277
  • 6,289
  • 17
  • 63
  • 132
0

To answer your question you should use this:

sort -t, -k1,1n -k2,2n yourFile.csv

The problem with your command is that -n does no apply to the fields you try to sort on; -k1,2n would do that but it sill does not solves your problem because it will consider both fields together (e.g. 10,10, 10,12) and will not work probably because of the you locale.

If you try

LC_ALL=C sort -t, -k1,2n yourFile.csv

you will get something like:

10,10,7.17,1.077383,0.00428382
10,12,7.45,1.177068,0.00390197
10,4,6.86,1.184806,0.00489828
10,6,6.98,1.106846,0.00463645
10,8,7.09,1.106254,0.00451672
12,10,6.71,1.224453,0.00506310
12,12,6.96,1.141856,0.00446641
12,4,6.41,1.510563,0.00590838
12,6,6.51,1.187841,0.00548915
12,8,6.62,1.217152,0.00532222

(ordered by first two fields 'concatenated').