3

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
1
  • Lines 1 and 2 should be in the correct position according to the command you give; the other out-of-order lines are, as others have pointed out, due to sorting as strings instead of numbers. Commented Jul 11, 2017 at 19:09

3 Answers 3

9

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.

Sign up to request clarification or add additional context in comments.

Comments

5

You can also combine fields 2-3 in one KEYDEF, e.g.

$ 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

Comments

1

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

I.e. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.