简体   繁体   中英

Sorting a file by multiple columns using bash sort

Here is the file I am working with

word01.2    10  25
word01.2    30  50
word01.1    10  30
word01.1    40  50
word01.2    40  50
word01.1    10  20
word01.1    5   8

When I try my sort command

sort -k1,1 -k2,2 -k3,3 file.txt 

I receive the following; I don't understand why line 2 and line 1 are not sorted, they should be in reverse positions

word01.1    10  30
word01.1    10  20
word01.1    40  50
word01.1    5   8
word01.2    10  25
word01.2    30  50
word01.2    40  50

When I try to add -g to the sort, the sorted file has more problems and column 1 is no longer sorted

sort -k1,1 -gk2,2 -gk3,3 file.txt 
word01.1    5   8
word01.1    10  20
word01.2    10  25
word01.1    10  30
word01.2    30  50
word01.1    40  50
word01.2    40  50

What I would like as results is

word01.1    5   8
word01.1    10  20
word01.1    10  30
word01.1    40  50
word01.2    10  25
word01.2    30  50
word01.2    40  50

You can also combine fields 2-3 in one KEYDEF, eg

$ sort -k1,1 -k2,3n file

Output

word01.1    5   8
word01.1    10  20
word01.1    10  30
word01.1    40  50
word01.2    10  25
word01.2    30  50
word01.2    40  50

You're missing the -n / --numeric-sort option, to sort according to string numerical value, not lexicographically (at least for second and third field):

$ sort -k1,1 -k2,2n -k3,3n file.txt
word01.1    5   8
word01.1    10  20
word01.1    10  30
word01.1    40  50
word01.2    10  25
word01.2    30  50
word01.2    40  50

Note that you can provide a global -n flag, to sort all fields as numerical values, or per key. Format for key is -k KEYDEF , where KEYDEF is F[.C][OPTS][,F[.C][OPTS]] and OPTS is one or more of ordering options , like n (numerical), r (reverse), g (general numeric), h (human numeric), etc.

Answer

 $ sort -k1,1 -k2,2n -k3,3n file.txt

provided by randomir is correct in every case.

Below mentioned combined option pasted by David C. Randin doesn't work corectly because it compare only first digit in third column.

$ sort -k1,1 -k2,3n file

Ie below pasted file won't be sorted correctly:

word01.2    10  25
word01.2    30  50
word01.1    10  30
word01.1    40  50
word01.2    40  50
word01.1    10  20
word01.1    5   8
word01.1    410  50
word01.1    45  120
word01.1    45  100
word01.1    45  1000
word01.1    40  6

I recommend first option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM