Sunday, July 25, 2021

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

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 sporting 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 like Redhat Linux, Solaris or IBM AIX because it's a 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

Without wasting any more of your time, here are sort command examples that show the power of this little Linux utility and how useful it can be to find information from text files and present them in a sorted manner. 


1) Sorting based on the numeric value of String using UNIX sort command:

unix sort, sort command in unixMany times instead of alphabetic sorting we need numeric sorting. Just like in the 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 command

Sometimes 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 a 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 columns using sort command as a sort -nk23 will sort the output first on the second column and then on the 3rd column.




4) Sorting output on alphabetical order by using UNIX sort command

In this example of the 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 the 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 the sorted output in UNIX

As you have seen in the 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 the 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 the 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:

  1. 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....

    ReplyDelete
  2. 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

    ReplyDelete
  3. 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?

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete