I have to sort the content of 2 files in such a way that I must first sort it by the dates and then within each subgroup sort them also by times. I tried achieving this by simply using

sort -k 2

and then

| sort -k 3

The 2nd line contains the dates and the 3rd one contains the times. The problem is that instead of actually sorting the values in each subgroup, after the second sort command, it just rearranges all the values only by the times, ignoring the previous sorting completely. For example, say I have the following input:

name1 3.5. 10:00

name2 3.5. 11:00

name3 3.5. 8:00

name4 4.5. 13:00

name5 5.5. 7:00

I would like to sort them by the times they have in each subgroup separately while the dates remain in the same order:

name3 3.5. 8:00

name1 3.5. 10:00

name2 3.5. 11:00

name4 4.5. 13:00

name5 5.5. 7:00

It would also be very helpful if the solution can be written in one line (using | for example).

Thank you in advance.

UPDATE:

I tried using sort -k2,2 -k3,3 but for whatever reason it STILL doesn't work! It sorts the dates correctly, but the times are out of order. If I add -n at the end, it sorts the times correctly, but then the dates are out of order... I even tried sorting the dates normally and the times numerically: sort -k2,2 -k3,3n but it STILL sorts only the dates correctly, while the times still remain out of order. I don't know what else to do. This is killing me...

1 Answers

0
e.dan On

Try this:

sort -k2,2 -k3,3 -n file

This means to sort on key 2 and then key 3, which seems to be what you want, and -n means to sort numerically, which obviously isn't going to really work with dates and times, but it's going to be closer to what you want than alpha-numeric sorting...