Sort CSV file by column priority using the "sort" command
Suppose you have another row 3;10;3
in your unsorted.csv
file. Then I guess you expect a numerically sorted result:
2;1;33;1;21;2;33;2;11;3;22;3;13;10;3
and not an alphabetically sorted one:
2;1;33;1;23;10;31;2;33;2;11;3;22;3;1
To get that, you have to use -n
:
sort --field-separator=';' -n -k 2,2 -k 1,1 -k 3,3 unsorted.csv
It is worth mentioning that 2,2
has to be used. If only 2
is used, then sort
takes the string from beginning of field 2 to the end. 2,2
makes sure that only field 2
is used.
Charlie's answer above didn't work for me on Cygwin (sort version 2.0, GNU textutils), the following did:
sort -t"," -k2 -k1 -k1