-1

I think there is probably some explanation but I just do not understand why it is behaving like this:

$ sort -k 2 -t "|" tests/resources/event1.txt
20180611|10|2|edf|8|abc
20180611|1|2|edf|1|abc
20180611|15|2|edf|15|abc
20180611|2|2|edf|2|abc
20180611|5|2|edf|1|abc
20180611|6|2|edf|1|abc
20180611|7|2|edf|8|abc
20180611|8|2|edf|3|abc

So why 10 comes before 1 and then 1 comes before 15? Either I should have 10, 15, 1 or 1,10,15 right? Why mixed up like this?

Junchao Gu
  • 1,615
  • 4
  • 22
  • 40

2 Answers2

1

From man sort:

-k, --key=POS1[,POS2]
       start a key at POS1 (origin 1), end it at POS2 (default end of line)

You are starting a key at the second column, but it continues to the end of the line. So for the 3 lines you asked about, the keys are:

102edf8abc
12edf1abc
152edf15abc

Looks sorted to me.

Perhaps you wanted -k 2,2?

e.dan
  • 6,761
  • 1
  • 22
  • 27
0

The issue with your command is that it considers the values as strings and sort accordingly which is correct in a way as 1 comes before 2. just add -n to your command which stands for Compare according to string numerical value or use the below command:

$ sort -n -k 2 -t "|" tests/resources/event1.txt

Output:

20180611|1|2|edf|1|abc
20180611|2|2|edf|2|abc
20180611|5|2|edf|1|abc
20180611|6|2|edf|1|abc
20180611|7|2|edf|8|abc
20180611|8|2|edf|3|abc
20180611|10|2|edf|8|abc
20180611|15|2|edf|15|abc

I hope this explains your questions.

Sumit S Chawla
  • 2,029
  • 1
  • 12
  • 25