How to sort the primary column with alphabetical order, and then the second column with numerical order?

Assuming a text file is present:

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 primary column with alphabetical order and the first column as the secondary column with numerical order. Below 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 -nand sort -k 2,2 -k 1,1 a.txt, but both of them gave the wrong results. What should I solve this problem? Thank.

+5
source share
2 answers

This should work:

sort -b -k2,2 -k1,1n

-bis significant, without it, the result is incorrect, since it sortincorrectly determines the position of the second column. See details man sort(or here ).

, locale. , sort.

+9

:

sort -k1.5,1.8 -k1.1,1.4n file
0

All Articles