| sort | |
|---|---|
The sort command | |
| Original author | Ken Thompson (AT&T Bell Laboratories) |
| Developers | Variousopen-source andcommercial developers |
| Initial release | November 3, 1971; 54 years ago (1971-11-03) |
| Written in | C |
| Operating system | Multics,Unix,Unix-like,V,Plan 9,Inferno,MSX-DOS,IBM i |
| Platform | Cross-platform |
| Type | Command |
| License | coreutils:GPLv3+ Plan 9:MIT License |
Incomputing,sort is a standardcommand line program ofUnix andUnix-likeoperating systems, that prints the lines of its input or concatenation of allfiles listed in itsargument 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 ofcommand-line options that can vary by implementation. For instance the "-r" flag will reverse the sort order. Sort ordering is affected by the environment'slocale settings.[1]
Asort command that invokes a general sort facility was first implemented withinMultics.[2] Later, it appeared inVersion 1 Unix. This version was originally written byKen Thompson atAT&T Bell Laboratories. ByVersion 4 Thompson had modified it to usepipes, but sort retained an option to name the output file because it was used to sort a file in place. InVersion 5, Thompson invented "-" to representstandard input.[3]
sort is part ofX/Open Portability Guide Issue 2 (1987). From there it was inherited intoPOSIX.[4]
The version ofsort bundled inGNUcoreutils was written by Mike Haertel and Paul Eggert.[1] This implementation employs themergesort algorithm. It offers an option to sort in parallel, though performance gain diminishes after 8 threads.[5]GNU parallel also provides a wrapper to perform parallel invocations ofsort with similar performance-gain characteristics: on a 48-core system, the speedup is about 3×.[6]
Thesort command has also been ported to theIBM i operating system, being accessible from the POSIX-compatibleQshell.[7]
Similar commands are available on many other operating systems, for example asort command is part ofASCII'sMSX-DOS2 Tools forMSX-DOS version 2.[8]
The "uutils" project provides a cross-platform implementation ofsort written inRust, with support for all of GNU coreutil's options. It uses thepar_sort_by orpar_sort_unstable_by function of Rayon, the Rust multi-threading library, implementing either an adaptive mergesort inspired bytimsort or a variation ofpattern-defeating quicksort.[9]
sort [OPTION]... [FILE]...
With noFILE, or whenFILE is-, the command reads fromstandard input.
In the table below, "Short" indicates only support for the one-letter (short) form of the option. Long options are originally a GNU extension and is not part of any version of SUS or POSIX. It has since also been adopted by FreeBSD.
| Name | Description | SUS/POSIX | Plan 9 | Inferno | FreeBSD | Linux | MSX-DOS | IBM i |
|---|---|---|---|---|---|---|---|---|
| -b, --ignore-leading-blanks | roIgnores leading blanks. | Short | Short | No | Yes | Yes | No | Short |
| -c, --check | Check that input file is sorted. | No | Short | No | Yes | Yes | No | Short |
| -C, --check=<silent|quiet> | Like -c, but does not report the first bad line. | No | No | No | Yes | Yes | No | No |
| -d, --dictionary-order | Considers only blanks and alphanumeric characters. | Short | Short | No | Yes | Yes | No | Short |
| -f, --ignore-case | Fold lower case to upper case characters. | Short | Short | No | Yes | Yes | No | Short |
| -g, --general-numeric-sort, --sort=general-numeric | Compares according to general numerical value. | Short | Short | No | Yes | Yes | No | No |
| -h, --human-numeric-sort, --sort=human-numeric | Compare human readable numbers (e.g., 2K 1G). | Short | No | No | Yes | Yes | No | No |
| -i, --ignore-nonprinting | Considers only printable characters. | Short | Short | No | Yes | Yes | No | Short |
| -k, --key=POS1[,POS2] | Start a key atPOS1 (origin 1), end it atPOS2 (default end of line) | No | No | No | Yes | Yes | No | No |
| -m | Merge only; input files are assumed to be presorted. | No | Short | No | Yes | Yes | No | Short |
| -M, --month-sort, --sort=month | Compares (unknown) < 'JAN' < ... < 'DEC'. | Short | Short | No | Yes | Yes | No | No |
| -n, --numeric-sort, --sort=numeric | Compares according to string numerical value. | Short | Short | Short | Yes | Yes | No | Short |
| -oOUTPUT | UsesOUTPUT file instead of standard output. | No | Short | No | Yes | Yes | No | Short |
| -r, --reverse | Reverses the result of comparisons. | Short | Short | Short | Yes | Yes | No | Short |
| -R, --random-sort, --sort=random | Shuffles, but groups identical keys. See also:shuf | No | 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 | Usesize for the maximum size of the memory buffer. | No | No | No | Yes | No | No | No |
| -tchar, --field-separator=char | Useschar instead of non-blank to blank transition. In other words, 'Tab character' separating fields ischar. | No | Short | No | Yes | Yes | No | Short |
| -Tdir, --temporary-directory=dir | Usesdir for temporaries. | No | Short | No | Yes | Yes | No | No |
| -u, --unique | Unique processing to suppress all but one in each set of lines having equal keys. | No | Short | No | Yes | Yes | No | Short |
| -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 |
$catphonebookSmith, Brett 555-4321Doe, John 555-1234Doe, Jane 555-3214Avery, Cory 555-4132Fogarty, Suzie 555-2314
$sortphonebookAvery, Cory 555-4132Doe, Jane 555-3214Doe, John 555-1234Fogarty, Suzie 555-2314Smith, Brett 555-4321
The-n option makes the program sort according to numerical value. Thedu command produces output that starts with a number, the file size, so its output can be piped tosort to produce a list of files sorted by (ascending) file size:
$du/bin/*|sort-n4 /bin/domainname24 /bin/ls102 /bin/sh304 /bin/csh
Thefind command with thels option prints file sizes in the 7th field, so a list of theLaTeX files sorted by file size is produced by:
$find.-name"*.tex"-ls|sort-k7n
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.
$catzipcodeAdam 12345Bob 34567Joe 56789Sam 45678Wendy 23456
$sort-k2nzipcodeAdam 12345Wendy 23456Bob 34567Sam 45678Joe 56789
The-k m,n option lets you sort on a key that is potentially composed of multiple fields (start at columnm, end at columnn):
$catquotafred 2000bob 1000an 1000chad 1000don 1500eric 500
$sort-k2,2n-k1,1quotaeric 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,-k1,1-t'|'zipcodeAdam|12345Wendy|23456Sam|45678Joe|56789Bob|34567
Sorting a file withtab separated values requires atab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation[10][11]to specify the tab as aC escape sequence.
$sort-k2,2-t$'\t'phonebookDoe, John555-1234Fogarty, Suzie555-2314Doe, Jane555-3214Avery, Cory555-4132Smith, Brett555-4321
The-r option just reverses the order of the sort:
$sort-rk2nzipcodeJoe 56789Sam 45678Bob 34567Wendy 23456Adam 12345
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 utilityshuf.
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.
sort(1) – Linux UserManual – User Commands from Manned.orgsort – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open GroupWords of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
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 '...'.