In computing, sort is a standard command line program of Unix and Unix-likeoperating systems, that prints the lines of its input or concatenation of all files listed in its argument list in sorted order. Sorting is done based on one or more sort keys extracted from each line of input. By default, the entire input is taken as sort key. Blank space is the default field separator. The command supports a number of command-line options that can vary by implementation. For instance the "-r" flag will reverse the sort order. Sort ordering is affected by the environment's locale settings.[1]
History
A sort command that invokes a general sort facility was first implemented within Multics.[2] Later, it appeared in Version 1 Unix. This version was originally written by Ken Thompson at AT&T Bell Laboratories. By Version 4 Thompson had modified it to use pipes, but sort retained an option to name the output file because it was used to sort a file in place. In Version 5, Thompson invented "-" to represent standard input.[3]
The version of sort bundled in GNUcoreutils was written by Mike Haertel and Paul Eggert.[1] This implementation employs the merge sort algorithm.
Similar commands are available on many other operating systems, for example a sort command is part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.[4]
The sort command has also been ported to the IBM i operating system.[5]
Syntax
sort [OPTION]... [FILE]...
With no FILE, or when FILE is -, the command reads from standard input.
Start a key at POS1 (origin 1), end it at POS2 (default end of line)
No
No
No
Yes
Yes
No
No
-m
Merge only; input files are assumed to be presorted.
No
Yes
No
Yes
Yes
No
Yes
-M, --month-sort, --sort=month
Compares (unknown) < 'JAN' < ... < 'DEC'.
Yes
Yes
No
Yes
Yes
No
No
-n, --numeric-sort, --sort=numeric
Compares according to string numerical value.
Yes
Yes
Yes
Yes
Yes
No
Yes
-oOUTPUT
Uses OUTPUT file instead of standard output.
No
Yes
No
Yes
Yes
No
Yes
-r, --reverse
Reverses the result of comparisons.
Yes
Yes
Yes
Yes
Yes
No
Yes
-R, --random-sort, --sort=random
Shuffles, but groups identical keys. See also: shuf
Yes
No
No
Yes
Yes
No
No
-s
Stabilizes sort by disabling last-resort comparison.
No
No
No
Yes
Yes
No
No
-Ssize, --buffer-size=size
Use size for the maximum size of the memory buffer.
No
No
No
Yes
No
No
No
-tx
'Tab character' separating fields is x.
No
Yes
No
No
Yes
No
Yes
-tchar, --field-separator=char
Uses char instead of non-blank to blank transition.
No
No
No
Yes
Yes
No
No
-Tdir, --temporary-directory=dir
Uses dir for temporaries.
No
Yes
No
Yes
Yes
No
No
-u, --unique
Unique processing to suppress all but one in each set of lines having equal keys.
No
Yes
No
Yes
Yes
No
Yes
-V, --version-sort
Natural sort of (version) numbers within text
No
No
No
Yes
Yes
No
No
-w
Like -i, but ignore only tabs and spaces.
No
Yes
No
No
No
No
No
-z, --zero-terminated
End lines with 0 byte, not newline
No
No
No
Yes
Yes
No
No
--help
Display help and exit
No
No
No
Yes
Yes
No
No
--version
Output version information and exit
No
No
No
Yes
Yes
No
No
/R
Reverses the result of comparisons.
No
No
No
No
No
Yes
No
/S
Specify the number of digits to determine how many digits of each line should be judged.
No
No
No
No
No
Yes
No
/A
Sort by ASCII code.
No
No
No
No
No
Yes
No
/H
Include hidden files when using wild cards.
No
No
No
No
No
Yes
No
Examples
Sort a file in alphabetical order
$ catphonebook
Smith, Brett 555-4321Doe, John 555-1234Doe, Jane 555-3214Avery, Cory 555-4132Fogarty, Suzie 555-2314
$ sortphonebook
Avery, Cory 555-4132Doe, Jane 555-3214Doe, John 555-1234Fogarty, Suzie 555-2314Smith, Brett 555-4321
Sort by number
The -n option makes the program sort according to numerical value. The du command produces output that starts with a number, the file size, so its output can be piped to sort to produce a list of files sorted by (ascending) file size:
The find command with the ls option prints file sizes in the 7th field, so a list of the LaTeX files sorted by file size is produced by:
$ find.-name"*.tex"-ls|sort-k7n
Columns or fields
Use the -k option to sort on a certain column. For example, use "-k 2" to sort on the second column. In old versions of sort, the +1 option made the program sort on the second column of data (+2 for the third, etc.). This usage is deprecated.
$ catzipcode
Adam 12345Bob 34567Joe 56789Sam 45678Wendy 23456
$ sort-k2nzipcode
Adam 12345Wendy 23456Bob 34567Sam 45678Joe 56789
Sort on multiple fields
The -k m,n option lets you sort on a key that is potentially composed of multiple fields (start at column m, end at column n):
$ catquota
fred 2000bob 1000an 1000chad 1000don 1500eric 500
$ sort-k2,2n-k1,1quota
eric 500an 1000bob 1000chad 1000don 1500fred 2000
Here the first sort is done using column 2. -k2,2n specifies sorting on the key starting and ending with column 2, and sorting numerically. If -k2 is used instead, the sort key would begin at column 2 and extend to the end of the line, spanning all the fields in between. -k1,1 dictates breaking ties using the value in column 1, sorting alphabetically by default. Note that bob, and chad have the same quota and are sorted alphabetically in the final output.
$ sort-k2,2-t$'\t'phonebookDoe, John 555-1234Fogarty, Suzie 555-2314Doe, Jane 555-3214Avery, Cory 555-4132Smith, Brett 555-4321
Sort in reverse
The -r option just reverses the order of the sort:
$ sort-rk2nzipcode
Joe 56789Sam 45678Bob 34567Wendy 23456Adam 12345
Sort in random
The GNU implementation has a -R --random-sort option based on hashing; this is not a full random shuffle because it will sort identical lines together. A true random sort is provided by the Unix utility shuf.
Sort by version
The GNU implementation has a -V --version-sort option which is a natural sort of (version) numbers within text. Two text strings that are to be compared are split into blocks of letters and blocks of digits. Blocks of letters are compared alpha-numerically, and blocks of digits are compared numerically (i.e., skipping leading zeros, more digits means larger, otherwise the leftmost digits that differ determine the result). Blocks are compared left-to-right and the first non-equal block in that loop decides which text is larger. This happens to work for IP addresses, Debian package version strings and similar tasks where numbers of variable length are embedded in strings.
^
Fowler, Glenn S.; Korn, David G.; Vo, Kiem-Phong. "KornShell FAQ". Archived from the original on 2013-05-27. Retrieved 3 March 2015. The $'...' string literal syntax was added to ksh93 to solve the problem of entering special characters in scripts. It uses ANSI-C rules to translate the string between the '...'.