[SOLVED] Sorting multiple columns with delimiter "." in bash

Issue

I’m trying to sort a list of text that looks like this:

2023.12.14
2020.10.4
2020.10.1
2020.5.18
2023.14.1
2021.1.1

desired output:

2020.5.18
2020.10.1
2020.10.4
2021.1.1
2023.12.14
2023.14.1

I tried to achieve it with the following command:

sort -t "." -k1,1 -k2,1 -k3,1 sortingTest.txt

With this command I’m trying to sort it by the first "column" (anything before the delimiter "."), and in case that two values are equal than compare the values of the second column etc.

For some reason it only sorts by comparing values of the first column.

What am I missing?

Solution

You got a few problems, the -k is defined as: -kfield1[,field2], which means -k2,1 is invalid. You can use -k1 -k2 -k3 to sort on the first three fields.

You properly want to do numeric sort on your fields, this can be enabled with -n see man 1 sort for other numeric sorting options:

$ sort -t. -n -k1 -k2 -k3 file.txt
2020.5.18
2020.10.1
2020.10.4
2021.1.1
2023.12.14
2023.14.1

Might work for you.

In case these are actually versions and not dates, then -V might be sufficient.

Answered By – Andreas Louv

Answer Checked By – Clifford M. (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.