Saturday, March 4, 2017

sort command in Unix and Linux with examples


Order plays an important role in our life, it makes things easier and manageable. Unix / Linux too have something to offer to "sort" text files. Text Processing utility  sort is very useful for this purpose .
lets see how it can help us -

I have a file called "Unix_Linux_Inventors.txt" it has following text inside it -

Ken Thompson
Linus Torvalds
Joe Ossanna
Dennis Ritchie
Douglas McIlroy





I tried to sort this file using sort command , it produced following output -



So now you have some idea how basic sort works

remember by default sort will not make any change in original file , it only displays you the sorted output on your terminal , however you  may actually want to replace original file with  sorted output or create a new file with the sorted output

in such situation you should use -o option of sort command like the example shown below -




To understand one more important feature of sort command ( numeric sort ) lets have a look at files inside my current directory with the help of  "ls -ltr"



Now with the help of cut command which i have explained in one of my previous post  ( cut command with examples ) , i have formatted the output of "ls -ltr"   -






Now if i sort this output using "sort" without any option , its going to produce following output


 

impressive ? Not really ? .... But i would say it is impressive ....
if you observe clearly ,sorting did work, first two rows are starting with 1 third with 2 and so on .

However  sometimes you may need your sorting to work purely on the basis of numbers and "-n" is specifically useful in such case , To understand how does it work , see below -








So this time it impressive even at first glance ☺,

but wait , What if you wanted this output in reverse order ? just use "-r" option , combined with "-n"






Now lets have look at another example

I have a file called "Shell_List.txt", it contains following text -

1,sh is a shell
3,ksh is a shell
5,csh is a shell
2,tcsh is a shell
4,bash is a shell
6,zsh is a shell






I want to sort this text file based upon alphabets in second column ( considering , as field separator  )

both plain "sort" and "sort -n" are not going to help -






however "sort" is going to sort out things for me as it have another options to do that "-t" and "-k"

 lets see how -





so to specify a certain pattern as a field pattern , you can use "-t " ( comma in this case ) and you can specify field position with -k option  and then sorting can be performed on the column you chose .

sorting is also useful when you have duplicate rows in your file .
I have a file called "Australia.txt" which contains following text

Sydney
Melbourne
Sydney
Brisbane
Perth
Perth
Darwin
Canberra






You can see both Sydney and Perth are appearing twice , with "-u" option , you can remove both sort file text and remove duplicate rows.




  "-c"  is another useful option which tells you if a file is already sorted or not, see below example -



I hope you all found this post useful, here are the links to my previous posts, do read them ,
and give your suggestions  , thanks for reading


if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com