I think you misunderstood the -k
option. The format of the value of the -k
parameter is the following: group.start_index,group.end_index
.
The index of the first character of a group is 1. The default value of start_index
is 1
. The default value of end_index
is the end of the line, not the end of the group.
sort -t '.' -k 3.1 -k 2.1 -k 1.1
In this first command, you are splitting your groups for sorting like so:
- Group 1 will be
31.12.2023
- Group 2 will be
12.2023
- Group 3 will be
2023
Since you didn't specify the end character, you can see it is using the full value for sorting.
In fact, you can add --debug
to your command, and it will show you more information.
$ sort -t '.' -k 3.1 -k 2.1 -k 1.1 --debug dates.txt
31.12.2023
____
_______
__________
__________
In your second command, it's a similar issue. However, the year group uses only the last digit of the year. In your example, it only uses 3
.
$ sort -t '.' -k 3.4 -k 2.1 -k 1.1 --debug dates.txt
31.12.2023
_
_______
__________
__________
Solution:
What you want is the following command:
sort -t '.' -k 3.1,3.4 -k 2.1,2.2 -k 1.1,1.2
This is how it looks with --debug
$ sort -t '.' -k 3.1,3.4 -k 2.1,2.2 -k 1.1,1.2 --debug dates.txt
31.12.2023
____
__
__
__________
As a matter of fact, as @jcaron mentions in the comment below, in this case, the result of your initial command and the one I mentioned as a solution will be the same. The last part of the value would already be sorted and will not affect sorting if you don't omit it.
sort -t '.' -k 3.1 -k 2.1 -k 1.1
Even better, as @FedKad suggests in the comment, you can even omit the starting index as it defaults to 1
anyway.
sort -t '.' -k 3 -k 2 -k 1