Sort CSV file by column priority using the “sort” command

限于喜欢 提交于 2019-12-02 14:25:31
sort --field-separator=';' --key=2,1,3

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

Suppose you have another row 3;10;3 in your unsorted.csv file. Then I guess you expect a numerically sorted result:

2;1;3
3;1;2
1;2;3
3;2;1
1;3;2
2;3;1
3;10;3

and not an alphabetically sorted one:

2;1;3
3;1;2
3;10;3
1;2;3
3;2;1
1;3;2
2;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.

..and if anyone followed the 'sort' solution but now wants to get more than the single unique entry per line (i.e. the top X number of unique entries), once you've sorted the file using 'sort', you can use a little app I created here:

https://github.com/danieliversen/MiscStuff/blob/master/scripts/findTopUniques.java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!