SYNOPSIS
sort [
sort [
DESCRIPTION
The sort command implements a full sort and merge facility. sort operates on input files containing records that are separated by the newline character.
If you do not specify either the
Options
-a -
allows files containing null characters to be treated as ASCII files rather than binary files.
-b -
skips, for comparison purposes, any leading white space (blanks or tabs) in any field (or key specification).
-c -
checks input files to ensure that they are correctly ordered according to the key position and sort ordering options specified, but does not modify or output the files. This option only affects the exit code.
-d -
uses dictionary ordering. sort examines only blanks, upper and lowercase letters, and numbers when making comparisons.
-f -
converts lowercase letters to uppercase for comparison purposes.
-i -
ignores, for comparison purposes, non-printable characters.
-k [startpos,[endpos]]-
specifies a sorting key. See the Sorting Keys section of this reference page for more information.
-M -
assumes that the field contains a month name for comparison purposes. Any leading white space is ignored. If the field starts with the first three letters of a month name in uppercase or lowercase, the comparisons are in month-in-year order. Anything that is not a recognizable month name compares less than JAN.
-m -
merges files into one sorted output stream. This option assumes that each input file is correctly ordered according to the other options specified on the command line; you can check this with the
-c option. -N -
specifies that the file being sorted does not have field delimiters.
-n -
assumes that the field contains an initial numeric value. sort sorts first by numeric value, then by the remaining text in the field, according to options. This option treats a field that contains no digits as if it had a value of zero. If more than one line contains no digits, the lines are sorted alphanumerically.
-o outfile-
writes output to the file outfile. By default, sort writes output onto the standard output. The output file can be one of the input files. In this case, sort makes a copy of the data to allow the (potential) overwriting of the input file.
-r -
reverses the order of all comparisons so that sort writes output from largest to smallest rather than smallest to largest.
-T tempdir-
specifies tempdir as the directory to use for sort's temporary files. When this option is not specified, sort stores its temporary file in the directory specified by the TMPDIR environment variable.
-t char-
indicates that the character char separates input fields. When you do not specify the
-t option, sort assumes that any number of white space characters (blank or tab) separate fields. -u -
ensures that output records are unique. If two or more input records have equal sort keys, sort writes only the first record to the output. When you use
-u with-c , sort prints a diagnostic message if the input records have any duplicates. -y [n]-
restricts the amount of memory available for sorting to n K of memory (where a K of memory is 1024 bytes). If n is missing, sort chooses a reasonable maximum amount of memory for sorting, dependent upon system configuration. sort needs at least enough memory to hold five records simultaneously. If you try to request less, sort automatically takes enough. When the input files overflow available memory, sort automatically does a polyphase merge (external sorting) algorithm which is, of necessity, much slower than internal sorting. n must be at least 2. n has a maximum value of 1024 and a default value of 250.
-z n-
indicates that the longest input record (including the newline character) is n characters in length. By default, record length is limited to 400 characters.
+ startposition[- endposition]-
is an obsolete method of specifying a sorting key. See the Sorting Keys section of this reference page for more information.
The
Sorting Keys
By default, sort examines entire input records to determine ordering. By specifying sorting keys on the command line, you can tell sort to restrict its attention to one or more parts of each record.
You can indicate the start of a sorting key with
-k m[.n][options]
where m and the optional n are positive integers.
You can choose options from the set
The number m specifies which field in the input record
contains the start of the sorting key.
The character given with the
- Note:
-
When you do not specify the
-t option, a field is considered to begin with the white space that separates it from the preceding field. When-t is specified, a field begins with the character following the separator.
You can also specify an ending position for a key, with
-k m[.n][options],p[.q][options]
where p and q are positive integers, indicating that the sort key ends with the qth character of the pth field. If you do not specify q or you specify a value of 0 for q, the sorting key ends at the last character of the pth field. For example,
-k 2.3,4.6
defines a sorting key that extends from the third character of the second
field to the sixth character of the fourth field.
The
sort also supports a historical method of defining the sorting key. Using this method, you indicate the start of the sorting key with
+m[.n][options]
which is equivalent to
-k m+1[.n+1][options]
You can also indicate the end of a sorting key with
-p[.q][options]
which when preceded with +m[.n] is equivalent to
-k m+1[.n+1],p.0[options]
if q is specified and is zero. Otherwise
-k m+1[.n+1],p+1[.q][options]
For example,
+1.2 -3.5
defines a sorting key with a starting position that sort
finds by skipping the first field and the first 2 characters of the next
field, its end position is found by skipping the first three fields and then
the first five characters of the next field.
In other words the sorting key extends from the third character of the
second field to the sixth character of the fourth field.
This is the same key as defined under the
With either syntax, if the end of a sorting key is not a valid position or no end was specified, the sorting key extends to the end of the input record.
You can specify multiple sort key positions by using several
When you specify the
EXAMPLES
To sort an input file having lines consisting of the day of the month, white space and the month, as in:
30 December 23 MAY 25 June 10 June
use the command:
sort -k 2M -k 1n
To merge two dictionaries, with one word per line:
sort -m -dfi dict1 dict2 >newdict
Often it is useful to explicitly specify both the starting and ending field
with the
sort -k 1,1 -k 2,2r file1
ENVIRONMENT VARIABLES
FILES
- /tmp/stm*
-
temporary files used for merging and
-o option. You can specify a different directory for temporary files using the TMPDIR environment variable. For further information, see envvar.
DIAGNOSTICS
Possible exit status values are:
- 0
-
Successful completion. Also returned if
-c is specified and the file is already in correctly sorted order. - 1
-
Returned if you specified
-c and the file is not correctly sorted. Also returned to indicate a non-unique record if you specified-cu . - 2
-
Failure due to any of the following:
- — missing key description after
-k - — more than one
-o option - — missing file name after
-o - — missing character after
-t - — more than one character after
-t - — missing number with
-y or-z - — endposition given before a startposition
- — badly formed sort key
- — invalid command line option
- — too many key field positions specified
- — insufficient memory
- — inability to open the output file
- — inability to open the input file
- — error writing to the output file
- — inability to create a temporary file or temporary file name
- — missing key description after
- Badly formed sort key position x
-
The key position was not specified correctly. Check the format and try again.
- file filename is binary
-
sort has determined that filename is binary because it found a NULL ('\0') character in a line.
- Insufficient memory for ...
-
This error normally occurs when you specify very large numbers for
-y or-z and there is not enough memory available for sort to satisfy the request. - Line too long: limit nn - truncated
-
Any input lines that are longer than nn. which is the default number of characters (400) or the number specified with the
-z option, are truncated. - Missing key definition after -k
-
You specified
-k , but did not specify a key definition after the-k . - Non-unique key in record: ...
-
With the
-c and-u options, a non-unique record was found. - Not ordered properly at: ...
-
With the
-c option, an incorrect ordering was discovered. - No newline at end of file
-
Any file not ending in a newline character has one added.
- Tempfile error on ...
-
The named temporary (intermediate) file could not be created. Make sure that you have a directory named /tmp and that this directory has space to create files. The directory for temporary files can be changed using the TMPDIR environment variable; see envvar.
- Tempnam() error
-
sort could not generate a name for a temporary working file. This should almost never happen.
- Temporary file error (no space) for ...
-
Insufficient space was available for a temporary file. Make sure that you have a directory named /tmp and that this directory has space to create files. The directory for temporary files can be changed using the ROOTDIR and TMPDIR environment variables; see envvar.
- Too many key field positions specified
-
This implementation of sort has a limit of 64 key field positions.
- Write error (no space) on output
-
Some error occurred in writing the standard output. This normally occurs when there is insufficient disk space to hold all of the intermediate data, or a diskette is write protected.
PORTABILITY
POSIX.2. x/OPEN Portability Guide 4.0. Windows 10. Windows Server 2016. Windows Server 2019. Windows 11. Windows Server 2022. Windows Server 2025.
Available on all UNIX systems, with only UNIX System V.2 or later having the full functionality described here.
The
LIMITS
The maximum number of key field positions is 64.
NOTES
The sortgen AWK script is a useful way to handle complex sorting tasks. It is described in the AWK Tutorial in the User's Guide. It originally appeared in The AWK Programming Language, by Aho, Weinberger, and Kernighan.
The sort command provided with PTC MKS Toolkit should not be confused with the Windows sort command.
AVAILABILITY
PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
SEE ALSO
PTC MKS Toolkit 10.5 Documentation Build 40.