Pages

Sorting is one of the essential tasks we always need to perform and UNIX or Linux has great support for sorting by using sort command. No matter what kind of shell script you are writing or you are looking for some information or reports many times you need to sort the output of one command or a set of line, with a combination of the sort with find command and grep in UNIX you can handle support request and report quite easily. In this UNIX sort command tutorial, we will see how we can use sort command in UNIX to achieve our sorting goal. This tutorial contains some of the practical example of sort command for sorting data in UNIX. With the use of “unique” and “sort” command, you can remove duplicates and present a sorted data in UNIX.Now let’s see what UNIX sort command can do for us using 5 examples of UNIX sort commands, I have tested these commands on Cygwin running on Windows XP and I expect it works fine on other OS e.g. Redhat Linux, Solaris or IBM AIX because it's pretty basic command, Please let me know if you face any issue while using these Unix sort examples on any other OS.I thought about writing on Sort command when I was working on 10 tips to work fast in Unix and UNIX command tutorial and Example for beginners but somehow I am publishing it quite late. Anyway, now you have Some Example of Sort command in UNIX to sort your files and directory as per your need.

UNIX or Linux Sort Command Examples

1) Sorting based on numeric value of String using UNIX sort command:Many times instead of alphabetic sorting we need numeric sorting. Just like in below example of Unix sort command if we want to sort based upon a numeric value of PID we can use sort -n along with sort -k(column). Since here PID is second column sort -nk2 will work for us. This is also another great example of UNIX sort by column, which allows you do sort the data based on any column in UNIX.

unix-sort-examples@unix-tutorial:~/test ps -ef | sort -nk2

UID PID PPID TTY STIME COMMAND

unix-sort-examples 500 2832 0 Jul 18 /usr/bin/bash

unix-sort-examples 1976 3556 2 Jul18 /usr/bin/ps

unix-sort-examples 2324 1 con Jul 18 /cthelper

unix-sort-examples 2676 1 con Jul 18 /cthelper

unix-sort-examples 2832 1 con Jul 18 /cthelper

unix-sort-examples 3332 2676 1 Jul 18 /usr/bin/bash

unix-sort-examples 3556 2324 2 Jul 18 /usr/bin/bash

2) Reverse sort by using UNIX sort commandSometimes we need to sort in reverse order e.g. descending order. sort -r option allows us to perform reverse sorting in Unix.

unix-sort-examples@unix-tutorial:~/test ps -ef | sort -rnk2

unix-sort-examples 3616 3556 2 11:49:43 /usr/bin/ps

unix-sort-examples 3556 2324 2 Jul 18 /usr/bin/bash

unix-sort-examples 3448 0 0 Jan 1 /usr/bin/ps

unix-sort-examples 3332 2676 1 Jul 18 /usr/bin/bash

unix-sort-examples 500 2832 0 Jul 18 /usr/bin/bash

UID PID PPID TTY STIME COMMAND

3) UNIX sort by column : Sorting based on any column in the input.sort command in Unix mostly used in combination of other Unix commands like find, grep, ls or ps and most of these commands produce output in tabular format and we want to sort based on any column. Unix sort command allows us to do this by using sort -k option. Let's see an example or Unix sort command to sort the output on any column we will use ps command output for this example and we will sort this output on column 2 (PID) and later on column 3 (PPID)

You can also sort based upon multiple columns using sort command as a sort -nk23 will sort the output first on the second column and then on 3rd column.

4) Sorting output on alphabetical order by using UNIX sort commandIn this example of UNIX sort command, we will see how to sort the output of any command in alphabetical order. Sort command in UNIX sorts the output in alphabetic order if you don't provide any options as shown in below example.

5) How to remove duplicates from sorted output in UNIXAs you have seen in above example of sort command in UNIX we have duplicates "stock trading" is coming two times. We can produce sorted output without duplicates in two ways in UNIX either by passing output of sort command to "uniq" command or by using sort -u option. Let’s see an example of sorting with unique elements using UNIX sort command:

That’s all for now on Unix sort command. Please share how are you using sort command in Unix , how useful you find UNIX sort command and is there any other alternative of Unix sort in other OS e.g. Redhat Linux, Solaris or IBM AIX ?