Sort (Unix)
- 5 min read
- 3 chapters
Related on Lably
Through Computing and Operating system
History
A sort command that invokes a general sort facility was first implemented within Multics. 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.
The version of sort bundled in GNU coreutils was written by Mike Haertel and Paul Eggert. 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.
The sort command has also been ported to the IBM i operating system.
Sign in to keep reading
2 more chapters are free with a Lably account.
Syntax
sort [OPTION]... [FILE]...
With no FILE, or when FILE is -, the command reads from standard input.
Parameters
Examples
Sort a file in alphabetical order
<span id="mwAhU"></span><span class="gp" id="mwAhY">$ </span>cat<span class="w" id="mwAhc"> </span>phonebook <span class="go" id="mwAhg">Smith, Brett 555-4321</span> <span class="go" id="mwAhk">Doe, John 555-1234</span> <span class="go" id="mwAho">Doe, Jane 555-3214</span> <span class="go" id="mwAhs">Avery, Cory 555-4132</span> <span class="go" id="mwAhw">Fogarty, Suzie 555-2314</span>
<span id="mwAh8"></span><span class="gp" id="mwAiA">$ </span>sort<span class="w" id="mwAiE"> </span>phonebook <span class="go" id="mwAiI">Avery, Cory 555-4132</span> <span class="go" id="mwAiM">Doe, Jane 555-3214</span> <span class="go" id="mwAiQ">Doe, John 555-1234</span> <span class="go" id="mwAiU">Fogarty, Suzie 555-2314</span> <span class="go" id="mwAiY">Smith, Brett 555-4321</span>
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:
<span id="mwAjA"></span><span class="gp" id="mwAjE">$ </span>du<span class="w" id="mwAjI"> </span>/bin/*<span class="w" id="mwAjM"> </span><span class="p" id="mwAjQ">|</span><span class="w" id="mwAjU"> </span>sort<span class="w" id="mwAjY"> </span>-n <span class="go" id="mwAjc">4 /bin/domainname</span> <span class="go" id="mwAjg">24 /bin/ls</span> <span class="go" id="mwAjk">102 /bin/sh</span> <span class="go" id="mwAjo">304 /bin/csh</span>
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:
<span id="mwAkQ"></span><span class="gp" id="mwAkU">$ </span>find<span class="w" id="mwAkY"> </span>.<span class="w" id="mwAkc"> </span>-name<span class="w" id="mwAkg"> </span><span class="s2" id="mwAkk">"*.tex"</span><span class="w" id="mwAko"> </span>-ls<span class="w" id="mwAks"> </span><span class="p" id="mwAkw">|</span><span class="w" id="mwAk0"> </span>sort<span class="w" id="mwAk4"> </span>-k<span class="w" id="mwAk8"> </span>7n
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.
<span id="mwAlg"></span><span class="gp" id="mwAlk">$ </span>cat<span class="w" id="mwAlo"> </span>zipcode <span class="go" id="mwAls">Adam 12345</span> <span class="go" id="mwAlw">Bob 34567</span> <span class="go" id="mwAl0">Joe 56789</span> <span class="go" id="mwAl4">Sam 45678</span> <span class="go" id="mwAl8">Wendy 23456</span>
<span id="mwAmI"></span><span class="gp" id="mwAmM">$ </span>sort<span class="w" id="mwAmQ"> </span>-k<span class="w" id="mwAmU"> </span>2n<span class="w" id="mwAmY"> </span>zipcode <span class="go" id="mwAmc">Adam 12345</span> <span class="go" id="mwAmg">Wendy 23456</span> <span class="go" id="mwAmk">Bob 34567</span> <span class="go" id="mwAmo">Sam 45678</span> <span class="go" id="mwAms">Joe 56789</span>
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):
<span id="mwAnM"></span><span class="gp" id="mwAnQ">$ </span>cat<span class="w" id="mwAnU"> </span>quota <span class="go" id="mwAnY">fred 2000</span> <span class="go" id="mwAnc">bob 1000</span> <span class="go" id="mwAng">an 1000</span> <span class="go" id="mwAnk">chad 1000</span> <span class="go" id="mwAno">don 1500</span> <span class="go" id="mwAns">eric 500</span>
<span id="mwAn4"></span><span class="gp" id="mwAn8">$ </span>sort<span class="w" id="mwAoA"> </span>-k2,2n<span class="w" id="mwAoE"> </span>-k1,1<span class="w" id="mwAoI"> </span>quota <span class="go" id="mwAoM">eric 500</span> <span class="go" id="mwAoQ">an 1000</span> <span class="go" id="mwAoU">bob 1000</span> <span class="go" id="mwAoY">chad 1000</span> <span class="go" id="mwAoc">don 1500</span> <span class="go" id="mwAog">fred 2000</span>
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.
Sorting a pipe delimited file
<span id="mwApA"></span><span class="gp" id="mwApE">$ </span>sort<span class="w" id="mwApI"> </span>-k2,2,-k1,1<span class="w" id="mwApM"> </span>-t<span class="s1" id="mwApQ">'|'</span><span class="w" id="mwApU"> </span>zipcode <span class="go" id="mwApY">Adam|12345</span> <span class="go" id="mwApc">Wendy|23456</span> <span class="go" id="mwApg">Sam|45678</span> <span class="go" id="mwApk">Joe|56789</span> <span class="go" id="mwApo">Bob|34567</span>
Sorting a tab delimited file
Sorting a file with tab separated values requires a tab character to be specified as the column delimiter. This illustration uses the shell's dollar-quote notation to specify the tab as a C escape sequence.
<span id="mwAqY"></span><span class="gp" id="mwAqc">$ </span>sort<span class="w" id="mwAqg"> </span>-k2,2<span class="w" id="mwAqk"> </span>-t<span class="w" id="mwAqo"> </span><span class="s1" id="mwAqs">$'\t'</span><span class="w" id="mwAqw"> </span>phonebook<span class="w" id="mwAq0"> </span> <span class="go" id="mwAq4">Doe, John 555-1234</span> <span class="go" id="mwAq8">Fogarty, Suzie 555-2314</span> <span class="go" id="mwArA">Doe, Jane 555-3214</span> <span class="go" id="mwArE">Avery, Cory 555-4132</span> <span class="go" id="mwArI">Smith, Brett 555-4321</span>
Sort in reverse
The -r option just reverses the order of the sort:
<span id="mwArg"></span><span class="gp" id="mwArk">$ </span>sort<span class="w" id="mwAro"> </span>-rk<span class="w" id="mwArs"> </span>2n<span class="w" id="mwArw"> </span>zipcode <span class="go" id="mwAr0">Joe 56789</span> <span class="go" id="mwAr4">Sam 45678</span> <span class="go" id="mwAr8">Bob 34567</span> <span class="go" id="mwAsA">Wendy 23456</span> <span class="go" id="mwAsE">Adam 12345</span>
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.

