Sort (Unix)

Last updated
sort
Sortunix.png
The sort command
Original author(s) Ken Thompson (AT&T Bell Laboratories)
Developer(s) Various open-source and commercial developers
Initial releaseNovember 3, 1971;50 years ago (1971-11-03)
Operating system Multics, Unix, Unix-like, V, Plan 9, Inferno, MSX-DOS, IBM i
Platform Cross-platform
Type Command
License coreutils: GPLv3+

In computing, sort is a standard command line program of Unix and Unix-like operating 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.

Contents

History

A sort command that invokes a general sort facility was first implemented within Multics. [1] 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. [2]

The version of sort bundled in GNU coreutils was written by Mike Haertel and Paul Eggert. [3] 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.

Parameters

NameDescription Unix Plan 9 Inferno FreeBSD Linux MSX-DOS IBM i
-b,
--ignore-leading-blanks
Ignores leading blanks.YesYesNoYesYesNoYes
-cCheck that input file is sorted.NoYesNoYesYesNoYes
-CLike -c, but does not report the first bad line.NoNoNoYesYesNoNo
-d,
--dictionary-order
Considers only blanks and alphanumeric characters.YesYesNoYesYesNoYes
-f,
--ignore-case
Fold lower case to upper case characters.YesYesNoYesYesNoYes
-g,
--general-numeric-sort,
--sort=general-numeric
Compares according to general numerical value.YesYesNoYesYesNoNo
-h,
--human-numeric-sort,
--sort=human-numeric
Compare human readable numbers (e.g., 2K 1G).YesNoNoYesYesNoNo
-i,
--ignore-nonprinting
Considers only printable characters.YesYesNoYesYesNoYes
-k,
--key=POS1[,POS2]
Start a key at POS1 (origin 1), end it at POS2 (default end of line)NoNoNoYesYesNoNo
-mMerge only; input files are assumed to be presorted.NoYesNoYesYesNoYes
-M,
--month-sort,
--sort=month
Compares (unknown) < 'JAN' < ... < 'DEC'.YesYesNoYesYesNoNo
-n,
--numeric-sort,
--sort=numeric
Compares according to string numerical value.YesYesYesYesYesNoYes
-oOUTPUTUses OUTPUT file instead of standard output.NoYesNoYesYesNoYes
-r,
--reverse
Reverses the result of comparisons.YesYesYesYesYesNoYes
-R,
--random-sort,
--sort=random
Shuffles, but groups identical keys. See also: shuf YesNoNoYesYesNoNo
-sStabilizes sort by disabling last-resort comparison.NoNoNoYesYesNoNo
-Ssize,
--buffer-size=size
Use size for the maximum size of the memory buffer.NoNoNoYesNoNoNo
-tx'Tab character' separating fields is x.NoYesNoNoYesNoYes
-tchar,
--field-separator=char
Uses char instead of non-blank to blank transition.NoNoNoYesYesNoNo
-Tdir,
--temporary-directory=dir
Uses dir for temporaries.NoYesNoYesYesNoNo
-u,
--unique
Unique processing to suppress all but one in each set of lines having equal keys.NoYesNoYesYesNoYes
-V,
--version-sort
Natural sort of (version) numbers within textNoNoNoYesYesNoNo
-wLike -i, but ignore only tabs and spaces.NoYesNoNoNoNoNo
-z,
--zero-terminated
End lines with 0 byte, not newlineNoNoNoYesYesNoNo
--helpDisplay help and exitNoNoNoYesYesNoNo
--versionOutput version information and exitNoNoNoYesYesNoNo
/RReverses the result of comparisons.NoNoNoNoNoYesNo
/SSpecify the number of digits to determine how many digits of each line should be judged.NoNoNoNoNoYesNo
/ASort by ASCII code.NoNoNoNoNoYesNo
/HInclude hidden files when using wild cards.NoNoNoNoNoYesNo

Examples

Sort a file in alphabetical order

$ cat phonebook Smith, Brett     555-4321Doe, John        555-1234Doe, Jane        555-3214Avery, Cory      555-4132Fogarty, Suzie   555-2314
$ sort phonebook 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:

$ du /bin/* | sort -n 4       /bin/domainname24      /bin/ls102     /bin/sh304     /bin/csh

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

$ cat zipcode Adam  12345Bob   34567Joe   56789Sam   45678Wendy 23456
$ sort -k 2n zipcode 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):

$ cat quota fred 2000bob 1000an 1000chad 1000don 1500eric 500
$ sort -k2,2n -k1,1 quota 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.

Sorting a pipe delimited file

$ sort -k2,2,-k1,1 -t'|' zipcode Adam|12345Wendy|23456Sam|45678Joe|56789Bob|34567

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 [6] [7] to specify the tab as a C escape sequence.

$ sort -k2,2 -t $'\t' phonebook  Doe, 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 -rk 2n zipcode 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.

See also

Related Research Articles

uniq is a utility command on Unix, Plan 9, Inferno, and Unix-like operating systems which, when fed a text file or standard input, outputs the text with adjacent identical lines collapsed to one, unique line of text.

ls

In computing, ls is a command to list computer files in Unix and Unix-like operating systems. ls is specified by POSIX and the Single UNIX Specification. When invoked without any arguments, ls lists the files in the current working directory. The command is also available in the EFI shell. In other environments, such as DOS, OS/2, and Microsoft Windows, similar functionality is provided by the dir command. The numerical computing environments MATLAB and GNU Octave include an ls function with similar functionality.

dir (command)

In computing, dir (directory) is a command in various computer operating systems used for computer file and directory listing. It is one of the basic commands to help navigate the file system. The command is usually implemented as an internal command in the command-line interpreter (shell). On some systems, a more graphical representation of the directory structure can be displayed using the tree command.

comm Standard UNIX utility for comparing files

The comm command in the Unix family of computer operating systems is a utility that is used to compare two files for common and distinct lines. comm is specified in the POSIX standard. It has been widely available on Unix-like operating systems since the mid to late 1980s.

head (Unix) Program on Unix and Unix-like systems

head is a program on Unix and Unix-like operating systems used to display the beginning of a text file or piped data.

The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file. Today, ar is generally used only to create and update static library files that the link editor or linker uses and for generating .deb packages for the Debian family; it can be used to create archives for any purpose, but has been largely replaced by tar for purposes other than static libraries. An implementation of ar is included as one of the GNU Binutils.

patch (Unix)

The computer tool patch is a Unix program that updates text files according to instructions contained in a separate file, called a patch file. The patch file is a text file that consists of a list of differences and is produced by running the related diff program with the original and updated file as arguments. Updating files with patch is often referred to as applying the patch or simply patching the files.

dd is a command-line utility for Unix, Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files.

join is a command in Unix and Unix-like operating systems that merges the lines of two sorted text files based on the presence of a common field. It is similar to the join operator used in relational databases but operating on text files.

tr (Unix) Command in Unix-like operating systems

tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.

wc (Unix)

wc is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.

split is a utility on Unix, Plan 9, and Unix-like operating systems most commonly used to split a computer file into two or more smaller files.

df (Unix) Standard Unix command

df is a standard Unix command used to display the amount of available disk space for file systems on which the invoking user has appropriate read access. df is typically implemented using the statfs or statvfs system calls.

paste is a Unix command line utility which is used to join files horizontally by outputting lines consisting of the sequentially corresponding lines of each file specified, separated by tabs, to the standard output.

du (Unix) Standard Unix program

du is a standard Unix program used to estimate file space usage—space used under a particular directory or files on a file system. A Windows commandline version of this program is part of Sysinternals suite by Mark Russinovich.

more (command)

In computing, more is a command to view the contents of a text file one screen at a time. It is available on Unix and Unix-like systems, DOS, Digital Research FlexOS, IBM/Toshiba 4690 OS, IBM OS/2, Microsoft Windows and ReactOS. Programs of this sort are called pagers. more is a very basic pager, originally allowing only forward navigation through a file, though newer implementations do allow for limited backward movement.

tail is a program available on Unix, Unix-like systems, FreeDOS and MSX-DOS used to display the tail end of a text file or piped data.

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.

The tsort program is a command line utility on Unix and Unix-like platforms, that performs a topological sort on its input. As of 2017, it is part of the POSIX.1 standard.

cat (Unix)

cat is a standard Unix utility that reads files sequentially, writing them to standard output. The name is derived from its function to concatenate files. It has been ported to a number of operating systems.

References

  1. "Multics Commands". www.multicians.org.
  2. McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
  3. "sort(1): sort lines of text files - Linux man page". linux.die.net.
  4. "MSX-DOS2 Tools User's Manual - MSX-DOS2 TOOLS ユーザーズマニュアル". April 1, 1993 via Internet Archive.
  5. IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). Retrieved 2020-09-05.
  6. "The GNU Bash Reference Manual, for Bash, Version 4.2: Section 3.1.2.4 ANSI-C Quoting". Free Software Foundation, Inc. 28 December 2010. Retrieved 1 February 2013. Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.
  7. 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 '...'.

Further reading