Friday, August 26, 2011

5 example of sort command in UNIX or Linux >>>> Unix Tutorial

Sorting is one of essential task 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 from one command or a set of line, with combination of sort with find command and grep in unix you can handle support request and reporting 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 practical example of sort command for sorting data in UNIX. With the use of “uniq” and “sort” command you can remove duplicates and present a sorted data in UNIX.




unix sort, sort command in unixNow 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 its 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 numeric value of PID we can use sort -n along with sort -k(column). Since here PID is second coloumn 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 command
Some time we need to sort in reverse order e.g. descending order. sort -r option allow 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 command produce output in tabular format and we want to sort based on any column. unix sort command allow 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)

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    2324       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    2564       0   0    Jan  1 /usr/bin/bash
unix-sort-examples    2676       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    2832       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    3332    2676   1    Jul 18 /usr/bin/bash
unix-sort-examples    3556    2324   2    Jul 18 /usr/bin/bash
unix-sort-examples    3764    3556   2  11:58:08 /usr/bin/ps

unix-sort-examples@unix-tutorial:~/test ps -ef | sort -nk3
     UID     PID    PPID TTY     STIME COMMAND
unix-sort-examples    2324       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    2676       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    2832       1 con    Jul 18 /cygdrive/c/Software/puttycyg-20101029/puttycyg-20101029/cthelper
unix-sort-examples    3556    2324   2    Jul 18 /usr/bin/bash
unix-sort-examples    3332    2676   1    Jul 18 /usr/bin/bash
unix-sort-examples     500    2832   0    Jul 18 /usr/bin/bash
unix-sort-examples     184    3556   2  11:58:21 /usr/bin/ps

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



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

unsorted output
unix-sort-examples@unix-tutorial:~/test cat names
stocks trading
futures trading
options trading
forex trading
electronic trading

sorted output
unix-sort-examples@unix-tutorial:~/test cat names | sort
electronic trading
forex trading
futures trading
options trading
stocks trading



5) How to remove duplicates from sorted output in UNIX
As 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:

Sorted output with duplicates
unix-sort-examples@unix-tutorial:~/test cat names | sort
electronic trading
forex trading
stocks trading
stocks trading

Sorted output without duplicates
unix-sort-examples@unix-tutorial:~/test cat names | sort | uniq
electronic trading
forex trading
stocks trading

unix-sort-examples@unix-tutorial:~/test cat names | sort -u
electronic trading
forex trading
stocks trading  



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 ?

Previous articles on Unix Linux commands and concepts:



5 comments :

cfajohnson said...

UUOC!!!

There's no need for cat in those examples; sort can take one or more filenames as arguments:

sort names
sort -u names
sort -r names
etc....

Rajiv said...

Good Unix Sort Tutorial. but Can you also help with following sorting examples in unix:
Unix Sort by Column
Unix Sort by date
Unix Sort by field
Unix Sort by size
This will be more useful. Thanks

Anonymous said...

i'm trying to do a simple sort in a file
typing my file name + four names at the command line and wanting it to show sorted on the screen?

Anonymous said...

This is what is in my text file

#!/bin/bash
if [ $# -eq 4 ]
then
echo "you have entered the following $# names:"
echo "$*"

else [ $# -nt 4 ]
echo "You Fool! You must enter four names when invoking this script"
echo "Have some coffee and try again!"


So at the command prompt I type my file name and 4 names

I wanted them sorted on the screen? Please help.

Anonymous said...

This should help :

#!/bin/bash
if [ $# -eq 4 ]
then
echo "you have entered the following $# names:"
echo "$*" | tr " " "\n" | sort
else [ $# -ne 4 ]
echo "You Fool! You must enter four names when invoking this script"
echo "Have some coffee and try again!"
fi

Post a Comment