Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use sort -k option / flag for advanced linux command line sorting

Sometimes you have a data file that needs sorting not just from left to right but by multiple values in the middle of each line. That is when you make use of the -k option / flag. 

Let's look at the following data file; let's call it test.txt. It starts with a timestamp in the German date format; meaning dd.mm.yyyy.

19.09.1942 12:34              Nekros
05.03.1945 20:15              Inaros
01.01.1969 23:55              Banshee
02.01.1969 20:15              Mesa
03.01.1969 12:00              Nezha
19.02.1969 17:38              Nova
06.03.1969 07:45              Excalibur
01.11.1969 08:00              Hydroid
02.09.1970 10:15              Rhino
29.07.1973 09:10              Atlas
08.04.2007 05:10              Wukong

Sorting all entries by date means I have to first sort by characters 7-10, then by characters 4-5 and lastly by 1-2. An additional hurdle is that we need numerical sorting for each of these groups rather than alphabetical sorting.

Sort by year

The first step is sorting by year. The sort option -k 1.7n looks for everything from character 7 until the first space and sorts it numerically.

cat test.txt | sort -k 1.7n

19.09.1942 12:34              Nekros
05.03.1945 20:15              Inaros
01.01.1969 23:55              Banshee
01.11.1969 08:00              Hydroid
02.01.1969 20:15              Mesa
03.01.1969 12:00              Nezha
06.03.1969 07:45              Excalibur
19.02.1969 17:38              Nova
02.09.1970 10:15              Rhino
29.07.1973 09:10              Atlas
08.04.2007 05:10              Wukong

If you want to have you command more concise you can add the range directly by using -k 1.7,1.10n instead of the implicit command above.

Sort by year and month

New we just have to add the addional sorting by month that gets executed after sorting by year has been done. Therefore we have to append the option -k 1.4,1.5n to our previous command. This means that we take the string from postion 4 to 5 and sort it numerically.

cat test.txt | sort -k 1.7n -k 1.4,1.5n

19.09.1942 12:34              Nekros
05.03.1945 20:15              Inaros
01.01.1969 23:55              Banshee
02.01.1969 20:15              Mesa
03.01.1969 12:00              Nezha
19.02.1969 17:38              Nova
06.03.1969 07:45              Excalibur
01.11.1969 08:00              Hydroid
02.09.1970 10:15              Rhino
29.07.1973 09:10              Atlas
08.04.2007 05:10              Wukong

Sort by year, month and day

And finally we add a third sorting option that will be executed after everything has been sorted by year and month. We add the option -k 1.1,1.2n that says: take characters 1 to 2 and sort them also numerically.

cat test.txt | sort -k 1.7n -k 1.4,1.5n -k 1.1,1.2n

19.09.1942 12:34              Nekros
05.03.1945 20:15              Inaros
01.01.1969 23:55              Banshee
02.01.1969 20:15              Mesa
03.01.1969 12:00              Nezha
19.02.1969 17:38              Nova
06.03.1969 07:45              Excalibur
01.11.1969 08:00              Hydroid
02.09.1970 10:15              Rhino
29.07.1973 09:10              Atlas
08.04.2007 05:10              Wukong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.