sort Command in Linux with Examples

This tutorial explains Linux “sort” command, options and its usage with examples.

sort – Sorts the lines in a text file.

Description :

Write sorted concatenation of all FILE(s) to standard output.

Usage :

sort [OPTION]… [FILE]…

Options :

advertisement
advertisement

Ordering options:

-b, –ignore-leading-blanks
ignore leading blanks
-d, –dictionary-order
consider only blanks and alphanumeric characters
-f, –ignore-case
fold lower case to upper case characters
-g, –general-numeric-sort
compare according to general numerical value
-i, –ignore-nonprinting
consider only printable characters
-M, –month-sort
compare (unknown) < `JAN' < ... < `DEC' -n, –numeric-sort
compare according to string numerical value
-r, –reverse
reverse the result of comparisons

Other options:

-c, –check
check whether input is sorted; do not sort
-k, –key=POS1[,POS2]
start a key at POS1, end it at POS 2 (origin 1)
-m, –merge
merge already sorted files; do not sort
-o, –output=FILE
write result to FILE instead of standard output
-s, –stable
stabilize sort by disabling last-resort comparison
-S, –buffer-size=SIZE
use SIZE for main memory buffer
-t, –field-separator=SEP
use SEP instead of non- to whitespace transition
-T, –temporary-directory=DIR
use DIR for temporaries, not $TMPDIR or /tmp multiple options specify multiple directories
-u, –unique
with -c: check for strict ordering
otherwise: output only the first of an equal run
-z, –zero-terminated
end lines with 0 byte, not newline

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

SIZE may be followed by the following multiplicative suffixes: % 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

With no FILE, or when FILE is -, read standard input.

Examples :

For example, here is a test file:

advertisement
$ cat test
zzz
sss
qqq
aaa
BBB
ddd
AAA

1. Basic Example

It sorts lines in test file and displays sorted output.

$ sort test
aaa
AAA
BBB
ddd
qqq
sss
zzz

2. Perform Numeric Sort using -n option

advertisement

If we want to sort on numeric value, then we can use -n or –numeric-sort option.

Consider following test file:

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

$ sort -n test
11 qqq
22 zzz
33 sss
55 BBB
77 aaa

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.

Consider the following test files:

$ 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).

$ sort -M test
jan
feb
mar11
apr
aug
sept
oct

4. Sort the passwd file by the 3rd field (numeric userid)

$ sort -t: -k 3n /etc/passwd | more
 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

5. Write sorted concatenation of all input files to standard output

If more that one file is provided as input, the sort command produces a sorted concatenation on stdout.

$ cat sort1.txt 
7
4
9
1
$ cat sort2.txt 
8
5
6
2
$ sort sort1.txt sort2.txt 
1
2
4
5
6
7
8
9

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

$ cat test
5
2
2
1
4
4
$ 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

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

$ cat test
2
5
1
6
$ sort -c test
sort: test:3: disorder: 1

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.