Power Of Linux Sort Command

August 26, 2013 | By
| Reply More

Sorting in Linux can be performed with a simple utility command sort. Of course, it sorts its input, alphabetically by default, though it can sort numerically as well. It takes input from STDIN by default, but can sort files when file names are provided as argument. This article explains sort command with examples.

Linux Sort command

Sort command can be invoked by typing just sort. It will then propmt for the input from STDIN. After enring at STDIN, ctrl+d is entered for merking end of input.

$ sort
cappa
beta
delta
theta
alpha

alpha
beta
cappa
delta
theta

Sort is a text processing tool, so it can be used by piping in some input from another command.

$ tail /etc/passwd | sort
altair:x:1001:1001:Altair Ibn La Ahad,,,,:/home/altair:/bin/bash
bind:x:120:132::/var/cache/bind:/bin/false
dnsmasq:x:121:65534:dnsmasq,,,:/var/lib/misc:/bin/false
mysql:x:115:129:MySQL Server,,,:/nonexistent:/bin/false
postfix:x:105:126::/var/spool/postfix:/bin/false
raghu:x:1000:1000:raghu,,,:/home/raghu:/bin/bash
smmsp:x:119:131:Mail Submission Program,,,:/var/lib/sendmail:/bin/false
smmta:x:118:130:Mail Transfer Agent,,,:/var/lib/sendmail:/bin/false
sshd:x:117:65534::/var/run/sshd:/usr/sbin/nologin
statd:x:116:65534::/var/lib/nfs:/bin/false

The sorting can be reversed with -r option.

$ tail /etc/passwd | sort -r
statd:x:116:65534::/var/lib/nfs:/bin/false
sshd:x:117:65534::/var/run/sshd:/usr/sbin/nologin
smmta:x:118:130:Mail Transfer Agent,,,:/var/lib/sendmail:/bin/false
smmsp:x:119:131:Mail Submission Program,,,:/var/lib/sendmail:/bin/false
raghu:x:1000:1000:raghu,,,:/home/raghu:/bin/bash
postfix:x:105:126::/var/spool/postfix:/bin/false
mysql:x:115:129:MySQL Server,,,:/nonexistent:/bin/false
dnsmasq:x:121:65534:dnsmasq,,,:/var/lib/misc:/bin/false
bind:x:120:132::/var/cache/bind:/bin/false
altair:x:1001:1001:Altair Ibn La Ahad,,,,:/home/altair:/bin/bash

Check for sorted input

The sort command can check if the input is already sorted or not with -c option. If it is not sorted, it returns the first unsorted line.

$ sort -c /etc/passwd
sort: /etc/passwd:2: disorder: daemon:x:1:1:daemon:/usr/sbin:/bin/sh

Nothing is printed for sorted input:

$ sort /etc/passwd | sort -c
$ echo $?
0

Numeric Sort

By default, sort treats all the characters as string characters. The numeric characters are also treated as characters. Let us see an example. First we create a file to be sorted:

$ cat > numeric.txt
01
10
25
83
502
111

Now, when we try to sort this file, we get:

$ sort numeric.txt
01
10
111
25
502
83

This is not what we generally expect. Of course 111 is larger than 25 and 502 is greater than 83 as well. But if you see closely, the numbers are sorted in dictionary order. For example, for 111 and 25, compare the first character, 1 precedes 2. Hence 111 is placed before 25.

If we want to sort numerically, we use -n option.

$ sort -n numeric.txt
01
10
25
83
111
502

Remove duplicate lines

With -u option, the duplicate lines are printed only once. Consider a test file for illustration.

$ cat testfile
zzz
zzz
hello
Linux
expertslogin
lInux
raghu
linux
world

First we sort this file

$ sort testfile
expertslogin
hello
linux
lInux
Linux
raghu
world
zzz
zzz

The last two lines are identical. With -u option, they will be printed only once.

$ sort -u testfile
expertslogin
hello
linux
lInux
Linux
raghu
world
zzz

Ignore Case

Like many other Linux tools, sort command is case sensitive by default. But if we need to ignore the case, we can use -f or --ignore-case option.
For example, in the above file, there are three instances of the word "linux" in different cases. By default, they all are printed with -u option. It means, they are not unique for sort command. But if we use -f option:

$ sort -f -u testfile
expertslogin
hello
Linux
raghu
world
zzz

Only one of these is printed.

Sort by column

The sort command can sort column-wise with -k option. For illustration of this option, we consider /etc/fstab file.

$ cat /etc/fstab
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0

$ sort /etc/fstab
devpts /dev/pts devpts gid=5,mode=620 0 0
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
LABEL=/boot /boot ext3 defaults 1 2
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0

The option -k3 will sort on the basis of third column (i.e. on the basis of filesystem type as in third column, filesystem type are mentioned)

$ sort -k3 /etc/fstab
devpts /dev/pts devpts gid=5,mode=620 0 0
/dev/VolGroup00/LogVol00 / ext3 defaults 1 1
LABEL=/boot /boot ext3 defaults 1 2
proc /proc proc defaults 0 0
/dev/VolGroup00/LogVol01 swap swap defaults 0 0
sysfs /sys sysfs defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0

Custom Field Separator

By default, delimiter for columns is whitespace or tab. But this can be changed with -t or --field-separator option. We will use colon (:) as the delimiter and /etc/passwd file as input.
If we want to sort the /etc/passwd file on the basis of UIDs, we use the following command.

$ sort -n -t ':' -k3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
<---output truncated--->

Month sort

The sort command can do amazing things such as sort monthly, i.e. JAN$ cat months.txt
sep
august
july
dec
feb
may
jan

$ sort -M months.txt
jan
feb
may
july
august
sep
dec

Human numeric sort

Another interesting sort is human numeric sort, i.e. sort can compare numbers like 2K, 5G, 3M etc.

$ cat human_numeric
4G
2K
3M
1G
34K
52M
200M

$ sort -h human_numeric
2K
34K
3M
52M
200M
1G
4G

Filed Under : LINUX COMMANDS

Free Linux Ebook to Download

Leave a Reply

Commenting Policy:
Promotion of your products ? Comment gets deleted.
All comments are subject to moderation.