Sort command is helpful to sort/order lines in text files. You can sort the data in text file and display the output on the screen, or redirect it to a file. Based on your requirement, sort provides several command line options for sorting data in a text file.
Sort Command Syntax:
$ sort [-options]
For example, here is a test file:
$ cat test zzz sss qqq aaa BBB ddd AAA
And, here is what you get when sort command is executed on this file without any option. It sorts lines in test file and displays sorted output.
$ sort test aaa AAA BBB ddd qqq sss zzz
1. Perform Numeric Sort using -n option
If we want to sort on numeric value, then we can use -n or –numeric-sort option.
Create the following test file for this example:
$ cat test 22 zzz 33 sss 11 qqq 77 aaa 55 BBB
The following sort command sorts lines in test file on numeric value in first word of line and displays sorted output.
$ sort -n test 11 qqq 22 zzz 33 sss 55 BBB 77 aaa
2. Sort Human Readable Numbers using -h option
If we want to sort on human readable numbers (e.g., 2K 1M 1G), then we can use -h or –human-numeric-sort option.
Create the following test file for this example:
$ cat test 2K 2G 1K 6T 1T 1G 2M
The following sort command sorts human readable numbers (i.e 1K = 1 Thousand, 1M = 1 Million, 1G = 1 Giga, 1T = 1 Tera) in test file and displays sorted output.
$ sort -h test 1K 2K 2M 1G 2G 1T 6T
3. Sort Months of an Year using -M option
If we want to sort in the order of months of year, then we can use -M or –month-sort option.
Create the following test file for this example:
$ cat test sept aug jan oct apr feb mar11
The following sort command sorts lines in test file as per month order. Note, lines in file should contain at least 3 character name of month name at start of line (e.g. jan, feb, mar). If we will give, ja for January or au for August, then sort command would not consider it as month name.
$ sort -M test jan feb mar11 apr aug sept oct
4. Check if Content is Already Sorted using -c option
If we want to check data in text file is sorted or not, then we can use -c or –check, –check=diagnose-first option.
Create the following test file for this example:
$ cat test 2 5 1 6
The following sort command checks whether text file data is sorted or not. If it is not, then it shows first occurrence with line number and disordered value.
$ sort -c test sort: test:3: disorder: 1
5. Reverse the Output and Check for Uniqueness using -r and -u options
If we want to get sorted output in reverse order, then we can use -r or –reverse option. If file contains duplicate lines, then to get unique lines in sorted output, “-u” option can be used.
Create the following test file for this example:
$ cat test 5 2 2 1 4 4
The following sort command sorts lines in test file in reverse order and displays sorted output.
$ sort -r test 5 4 4 2 2 1
The following sort command sorts lines in test file in reverse order and removes duplicate lines from sorted output.
$ sort -r -u test 5 4 2 1
6. Selectively Sort the Content, Customize delimiter, Write output to a file using -k, -t, -o options
If we want to sort on the column or word position in lines of text file, then “-k” option can be used. If we each word in each line of file is separated by delimiter except ‘space’, then we can specify delimiter using “-t” option. We can get sorted output in any specified output file (using “-o” option) instead of displaying output on standard output.
Create the following test file for this example:
$ cat test aa aa zz aa aa ff aa aa tt aa aa kk
The following sort command sorts lines in test file on the 3rd word of each line and displays sorted output.
$ sort -k3 test aa aa ff aa aa kk aa aa tt aa aa zz
$ cat test aa|5a|zz aa|2a|ff aa|1a|tt aa|3a|kk
Here, several options are used altogether. In test file, words in each line are separated by delimiter ‘|’. It sorts lines in test file on the 2nd word of each line on the basis of numeric value and stores sorted output into specified output file.
$ sort -n -t'|' -k2 test -o outfile
The contents of output file are shown below.
$ cat outfile aa|1a|tt aa|2a|ff aa|3a|kk aa|5a|zz
Comments on this entry are closed.
Great article.. Thanks!!!!
Hi,
Thanks a lot..
Easy but very useful article
in the last example – $sort -n -t’|’ -k2 test -o outfile – – – i changed ‘-k2’ to ‘-k3’ and expected to see:
aa|2a|ff
aa|3a|kk
aa|1a|tt
aa|5a|zz
and changed the output from ‘outfile’ to ‘outfile1’ but the results were the same.
also ‘-h’ did not work for me.
Really your articles are very useful and simple. I am learning linux from your commands. Thanks please maintain it.
THANKS for your intereting artical.
Very useful skills to know, which is simple an easy to understand.
I will play little with this commands to learn and comfortable.
//Chris
Hi Himanshu,
Thanks for this topic.
May I know the detailed reason why lowercase prededes uppercase, as in:
$ sort test
aaa
AAA
BBB
ddd
qqq
sss
zzz
As long as I remember, from ASCII to UTF-8, uppercase has smaller code value than lowercase, so how come lowercase comes first?
— Philippe
Ok, got it. It depends on [locale] settings, ( on LC_COLLATE )
for instance:
LC_COLLATE=C sort data
AAA
BBB
SSS
aaa
ddd
qqq
sss
– -Philippe
You have left out multiple fields, field ranges and character selection with -k, e.g. -k1,2 -k1.9,1.11n
Vern, remove -n from your command. With it, you are sorting numerically, and, since there are no digits, all the fields evaluate to 0.
Vern, you couldn’t change the order because you are still using the flag -n (which is the numeric sort).
Remove that and I will work.
Cheers
Juan
Thanks a lot.
If you could include sort by column, that would be great
example sort -k1.5,1.10 file > sortfile
As for “-h”, I think that’s a relatively recent feature, so older version of sort doesn’t have it. My system has sort version 8.20, which does have “-h” option.
__
sol
Great article.
sort — human-numeric-test doesn’t seem to exist on Mac OS X.
Your explanation for every command is very simple and easy to understand. Thanks!!
Two of my favorite sort tricks:
1) sort -bdfu : sort, ignoring leading blanks and any non-alphanumeric characters, treating upper and lower case the same; keep only one occurrence of identical lines.
2) sort -t. -k 1n -k 2n -k 3n -k 4n
Sort a list of IPv4 addresses (such as /etc/hosts) in numerical order.
Example:
192.168.10.1 gateway-10
192.168.1.2 router-2
192.168.20.1 server-20-1
192.168.2.2 server-2-2
192.168.10.2 gateway-10-alt
192.168.1.1 router-1
After “sort”, you get a strange order, since “0” comes before “.”.
192.168.10.1 gateway-10
192.168.10.2 gateway-10-alt
192.168.1.1 router-1
192.168.1.2 router-2
192.168.20.1 server-20-1
192.168.2.2 server-2-2
After sort -t. -k 1n -k 2n -k 3n -k 4n
192.168.1.1 router-1
192.168.1.2 router-2
192.168.10.1 gateway-10
192.168.10.2 gateway-10-alt
192.168.2.2 server-2-2
192.168.20.1 server-20-1
Even better, these commands can be used inside vi/vim. Put the cursor on the first line you want to sort, and type “!}sort -bdfu”. When you hit enter, all lines up to the next blank line are passed to the sort command, and inserted in place of the original.
cat > file
dD
Dc..
I expected the o/p aftr SORT FILE to stay same as i/p…Bt I gt it as
Dc
dD..Generally , wen I try to sort 2 letters d and D, d is displayed 1st followed by D..
M confused about wat format is followed fr sorting(ascii ??)..Can some 1 pls help me with this?
VIVEK, sort will use the collating order of your locale.
This may be aAbBcCdD… or AaBbCcDd… or, in the POSIX/C locale which is recommended for scripting, ABCD….abcd.
The best thing to use is: export LC_ALL=C
does anyone know hot to sort selected lines like for example i have 50 lines and i only need to see first 10 lines. is it possible??? pls help me
Anar, use head:
head “$file” | sort
Or, if you want to sort the first ten lines and keep the rest as they are:
{
head | sort
cat
} < "$file"
Hi,
I’m trying to sort some data into a specific order. For example I have the following
21
21A
21AB
CD
25
25ZC
XY
45A
What I actually want is this :
21A
21AB
25ZC
45A
@Sumesh
So, what did you try, and what were the results?
By the way, you did not explained why following values (21, CD, XY, 25) are missing in you expected output list?
Hi,
I need to display only those values which is the combination of Alpha-Numeric.
And remove the values which is only alphabet or numeric values.
Hope this will clear.
Thanks,
Sumesh.
That’s clearer.
So, you need not only to sort, but also to filter data.
To filer, you could use [grep] command, and to filter you could use [sort] command
And you could use a “pipe” | between these two commands.
STFW (Search The Fantastic Web) and you will find what you need.
When you will have tried something, show us, in order to help you after you helped you first. 😉
–P
Help me the command as you mentioned, becuase I am new to unix.
Help me for writing this type of command. Conside this values are availble in “test.txt” file.
Write a bash script that takes one or more file names on the command line and prints out:
1. The name of the file
2. The number of lines & words in the file (NOT characters)
3. A list of all the words in the file and how many times they occur
(ideally, the words should all be lower cased)
Hint: you you will probably need to learn about pipe, loops, variables, echo, wc, cat, tr, sort, & uniq.
We can add commands to sort a list of IP addresses in a file:
cat ip_list.txt | sort -t”.” -k1n -k2n -k3n -k4n