Score:6

How to sort by year-month-day from ascending using the "sort" command?

br flag

I want to sort dates in the format 31.12.2023. Lowest year should be displayed first then lowest month then lowest day.

Is the following code fitting my needs? Seems good on first look.

sort -t '.' -k 3.1 -k 2.1 -k 1.1

What makes more sense to me though is:

sort -t '.' -k 3.4 -k 2.1 -k 1.1

But this does not sort the way I want.

EDIT:

Thx for your replies!

@Dan thx for your really detailed explanation. Still have no clue how to struggle with the key option exactly. I tested your code but it mixed still some things up. Maybe it's the way I use it in the variable or maybe the type of content in the strings.

Tried then the hint from @FedKad and this seems working like my -k 3.1 -k 2.1 -k 1.1. Does it mean the same thing as -k 3 -k 2 -k 1?

At the moment I only have entries from 2023 so cant evaluate if year also works but for now with above keys I got sorted how I want it. Maybe it was a fail how I described what I need as result but now I got day lowest to highest and month lowest to highest. For year I will have to wait until 2024 comes :) Here is my output example

07.01.2023

08.01.2023

09.01.2023

21.01.2023

26.01.2023

27.01.2023

02.02.2023

Anthony Kelly avatar
ng flag
`-k 3.4` would mean you are only sorting but the fourth character of the year field whereas `-k 3.1` will sort by the whole year field.
Score:9
jp flag
Dan

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
Raffa avatar
jp flag
+1 … Very graphic … In a good way :-)
FedKad avatar
cn flag
And what is the difference between your answer and this: `sort -t '.' -k 3 -k 2 -k 1` ?
jcaron avatar
me flag
+1 for the good explanation and the `--debug` option, but in this case, the end result of `-k 3.1 -k 2.1 -k 1.1` will be the same as `-k 3.1,3.4 -k 2.1,2.2 -k 1.1,1.2` anyway, the additional characters will have no influence on the sort since they are actually already sorted.
jp flag
Dan
@jcaron That is true. I only wanted to clarify to OP how the `-k` option works. But you're right, I should mention what you said in the answer
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.