Linux Sort Command

Howto sort files with the sort command

Linux Sort Command Examples


The sort command in Linux allows you to sort files or standard input line by line into a specific criteria. In its simplest form, you can sort in alphabetical order or numeric order. However, by adding various parameters to the command you can tailor your sort results with ease. In the following examples, we will look at some of the various parameters that can be passed.



Sorting numbers


For the following example, I have created a test file called "numbers.txt". The content of the number.txt file is as follows:



john@john-desktop:~/test_examples$ cat numbers.txt
2
8
6
4
1
9
3
5
7
john@john-desktop:~/test_examples$ sort numbers.txt
1 2 3 4 5 6 7 8 9

The next example introduces the reverse order parameter that can be passed "sort -r" :



john@john-desktop:~/test_examples$ sort -r numbers.txt
9
8
7
6
5
4
3
2
1

Sometimes the default search when used against numbers can return some strange results. For example, if you have numbers like "1, 10, 100, 2, 20, 200". When the default sort is run against these, you will see that the numbers "10 and 100" appear before the numbers "2 and 3....."



john@john-desktop:~/test_examples$ cat numbers2.txt
2
1
10
200
20
100
300
3
30
john@john-desktop:~/test_examples$ sort numbers2.txt
1
10
100
2
20
200
3
30
300

To rectify this problem we can issue the "-n" parameter. This specifies that we are sorting numbers numerically:



john@john-desktop:~/test_examples$ cat numbers2.txt
2
1
10
200
20
100
300
3
30
john@john-desktop:~/test_examples$ sort -n numbers2.txt
1
2
3
10
20
30
100
200
300


Sorting files Alphabetically


Sort by default will sort files in an alphabetical order:



john@john-desktop:~/test_examples$ cat text1.txt
beautiful
decoration
colour
a
john@john-desktop:~/test_examples$ sort text1.txt
a
beautiful
colour
decoration

Check to see if a file is sorted


By passing the "-c" parameter to the sort command, we can quickly tell if a file is in order. In the example below, we have two files "sorted.txt" and "unsorted.txt".



john@john-desktop:~/test_examples$ cat sorted.txt
Ape
Bird
Cat
Dog
Elephant
Fox
Horse
john@john-desktop:~/test_examples$ cat unsorted.txt
Horse
Elephant
Dog
Ape
Cat
Bird
Fox
john@john-desktop:~/test_examples$ sort -c unsorted.txt
sort: unsorted.txt:2: disorder: Elephant
john@john-desktop:~/test_examples$ sort -c sorted.txt
john@john-desktop:~/test_examples$ 

In the above example, we can see the words"disorder". This indicates that the file is not in a sorted order.


Sorting Multiple Files


So far we have seen that wee can use the sort command with a single parameter and a single file. However, it is possible to use multiple parameters and files. Multi file sort:



john@john-desktop:~/test_examples$ cat file1.txt
America
Canada
Japan
England
Wales
Germany
Scotland
Ireland
john@john-desktop:~/test_examples$ cat file2.txt
Spain
France
Holland
Russia
Turkey
Australia
john@john-desktop:~/test_examples$ sort file1.txt file2.txt
America
Australia
Canada
England
France
Germany
Holland
Ireland
Japan
Russia
Scotland
Spain
Turkey
Wales

Sorting files with multiple fields


So far we have seen some simple examples run against files that contain either one word or one number per line. In most situations, files will contain multiple entries on the same line. Thankfully, the sort command can handle sorting on fields with ease. Lets take a look at some examples:

Basic Sort on a file



john@john-desktop:~/test_examples$ cat multifile1.txt
John,21
Jessie,8
Fred,2
Maria,34
Paul,19
john@john-desktop:~/test_examples$ sort multifile1.txt
Fred,2
Jessie,8
John,21
Maria,34
Paul,19

As we can see in the above example, the standard sort here has sorted the output by the first field.

Sorting on fields


In the next example, we are going to specify a delimiter to sort by. In our example file, fields are separated by a ",". The parameter for the delimiter is the -t. The parameter -k is used to specify a key. The syntax here is "-kx,y" where "x" is the starting position and "y" is the end key. By default if "y" is not specified, then this will be set to "End of Line".



john@john-desktop:~/test_examples$ cat multifile1.txt
John,21
Jessie,8
Fred,2
Maria,34
Paul,19
john@john-desktop:~/test_examples$ sort multifile1.txt
Fred,2
Jessie,8
John,21
Maria,34
Paul,19
john@john-desktop:~/test_examples$ sort -t"," -k2n,2 multifile1.txt
Fred,2
Jessie,8
Paul,19
John,21
Maria,34

Now this time, we will sort the file on the 2nd field in reverse numerical order:



john@john-desktop:~/test_examples$ sort -t"," -k2nr,2 multifile1.txt
Maria,34
John,21
Paul,19
Jessie,8
Fred,2

The next example will sort alphabetically on the first field and then numerically on the 2nd field:



john@john-desktop:~/test_examples$ cat multifile2.txt
file1,1
file1,9
file2,1
file2,8
file2,4
file1,3
file1,2
file2,3
john@john-desktop:~/test_examples$ sort -t"," -k1,1 -k2n,2 multifile2.txt
file1,1
file1,2
file1,3
file1,9
file2,1
file2,3
file2,4
file2,8


john@john-desktop:~/test_examples$ cat multifile6.txt
192.168.0.1
127.0.0.10
127.0.0.1
127.0.0.2
192.168.0.100
192.168.0.2
john@john-desktop:~/test_examples$ sort -t"." -k2,2n -k4,4n multifile6.txt
127.0.0.1
127.0.0.2
127.0.0.10
192.168.0.1
192.168.0.2
192.168.0.100

Remove duplicate entries with sort


To remove duplicate entries, we can specify the parameter -u:



john@john-desktop:~/test_examples$ sort multifile4.txt
AIX
AIX
AS400
Linux
Linux
Puppy
Solaris
Solaris
VMS
Windows
Windows
Zos
Zos
john@john-desktop:~/test_examples$ sort -u multifile4.txt
AIX
AS400
Linux
Puppy
Solaris
VMS
Windows
Zos

In the above, the "-u" parameter removed the duplicate entries.


There are numerous other parameters that can be passed to the sort command. To discover more about these, you can issue the command:
info coreutils 'sort invocation'

Here you will find a description of all the available parameters and a description of their functionality.