Linux Sort Command Examples
The Linux Sort can be used to sort the content of a text file or output from other commands, By default, the sort separating filed or the sort delimiter is just space, but you’ll be able to determine a secondary field delimiter if you wish (comma or colon). Output by sort is written to the screen unless you redirect it to a text file or command to process it further.
The basic flags are given below:
- -f - create all lines capital prior to sorting (so “Tom” and “tom” are dealt the equivalent).
- -r - Sort in reversal arrange (so “zee” begins the list instead of “A”).
- -k - defines the column according to which the data is sorted.
- -na - Sort a column in numeric order.
- -tx - apply x for the field delimiter (substitute x with a comma or a different character).
- -u - repress each but single line in every set of lines with same sort fields (thus if you sort on a field carrying surnames, just one “Steve” will look yet if there are a lot of).
Sort Command Examples
Sort the output from “df -k” command by directory size:
# df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 26426068 6286364 20139704 24% /
tmpfs 3054216 12 3054204 1% /dev/shm
/dev/sdb1 28739340 2245772 26493568 8% /opt/nxtn
/dev/sdb2 258341356 157579592 100761764 61% /opt/nxtn/mysql/data
/dev/sda4 2803236 557652 2245584 20% /opt/nxtn/mysql/log
/dev/sda3 35751544 13977248 21774296 40% /var
# df -k | sort -k 2
tmpfs 3054216 12 3054204 1% /dev/shm
/dev/sda4 2803236 557652 2245584 20% /opt/nxtn/mysql/log
/dev/sda2 26426068 6286364 20139704 24% /
/dev/sdb1 28739340 2245880 26493460 8% /opt/nxtn
/dev/sda3 35751544 13977380 21774164 40% /var
/dev/sdb2 258341356 157579592 100761764 61% /opt/nxtn/mysql/data
Filesystem 1K-blocks Used Available Use% Mounted on
By default sort output is in ascending order, to get output in descending order we can use the -r switch:
# df -k | sort -k 2 -n -r
/dev/sdb2 258341356 157580556 100760800 61% /opt/nxtn/mysql/data
/dev/sda3 35751544 13977576 21773968 40% /var
/dev/sdb1 28739340 2246028 26493312 8% /opt/nxtn
/dev/sda2 26426068 628636 4 20139704 24% /
tmpfs 3054216 12 3054204 1% /dev/shm
/dev/sda4 2803236 557652 2245584 20% /opt/nxtn/mysql/log
Filesystem 1K-blocks Used Available Use% Mounted on
To use a delimiter other than space use the -t switch. Suppose we have the following text file and we want to sort it in ascending order based on the data in 3rd column.
1.1.3.1
4.5.2.2
2.2.2.2
2.8.9.2
0.2.1.2
as the delimiter here is ‘.’ instead of a space, so we will use:
# sort file.txt -t : -k 3
0.2.1.2
1.1.3.1
2.2.2.2
2.8.9.2
4.5.2.2
A very useful feature is that you can also sort the data based on two columns, e.g. to sort the above file based on column 3 and than column 1 use:
# sort file.txt -t : -k 3,2
0.2.1.2
1.1.3.1
2.2.2.2
2.8.9.2
4.5.2.2
To save the output from sort, you can simply use tohe -o option:
# sort file.txt -t : -k 3,2 -o /tmp/output
If the input data to sort contains alphabets than to Sort in numeric order: use the sort -n switch.