Assuming there is a text file:
10 A QAZ
5 A EDC
14 B RFV
3 A WSX
7 B TGB
I want to sort it with the second column as the main column with alphabet order and the first column as the secondary column with numeric order. The following is the expected result:
3 A WSX
5 A EDC
10 A QAZ
7 B TGB
14 B RFV
I tried sort -k 2,2 -k 1,1 a.txt -n and sort -k 2,2 -k 1,1 a.txt but both give the wrong results. What should I solve this problem? Thanks.
This should work:
The
-bis essential, without it, the output is wrong, sincesortwrongly determines the position of the second column. Seeman sort(or here) for details.Also, check your
locale. They can influence howsortworks.