sort - sort and collate lines
sort [ -bdfiMnr ] [ -tc ] [ sort-field ...] [ -cmu ] [ -o[ ]output-file
] [ -T directory ]
[ -y kmem ] [ -z recsz ] filename...
SYSTEM V SYNOPSIS
/usr/5bin/sort [ -bdfiMnr ] [ -tc ] [ sort-field ...] [ -cmu ] [
-o[ ]output-file ] [ -T directory ] [ -y kmem ] [ -z recsz ] file-
The System V version of this command is available with the System V
software installation option. Refer to for information on how to
install optional software.
The sort program sorts and collates lines contained in the named files,
and writes the result onto the standard output. If no filename argu-
ment is given, or if `-' appears as an argument, sort accepts input
from the standard input.
Output lines are normally sorted on a character-by-character basis,
from left to right within a line. The default collating sequence is
the ASCII character set. Lines can also be sorted according to the
contents of one or more fields specified by a sort-field, specifica-
tion, using the +sw (starting-word), -ew (end-at-word), and the -tc
(set-TAB-character/word delimiter) options, as described under OPTIONS
below. When no word delimiter is specified, one or more adjacent
white-space characters (SPACE and TAB) signify the end of the previous
word; the lines:
are collated as:
Each sort-field is evaluated in command-line order; later fields are
applied to the sorting sequence only when all earlier fields compare
equally. When all specified fields compare equally between two or more
lines, that subset of lines is sorted on a character-by-character
basis, from left to right.
SYSTEM V DESCRIPTION
When no fields are specified in the command line, the System V version
of sort treats leading blanks as significant, even with the -n (numeric
collating sequence) option; the lines:
are collated as:
-b Ignore leading SPACE characters when determining the starting
and ending positions of a field.
-d Dictionary order. Only letters, digits and the white-space
characters SPACE and TAB are significant in comparisons.
-f Fold in lower case. Treat upper- and lower-case letters equally
in collating comparisons.
-i Ignore characters outside the ASCII range 040-0176 in non-
-M Month order. The first three non-blank characters of the field
are folded to upper case and collated according to the sequence:
JAN FEB ... DEC. Field values outside this range appear ear-
lier than JAN. The -M option implies the -b option.
-n Numeric collating sequence. An initial numeric string, consist-
ing of optional blanks, optional minus signs, and zero or more
digits with an optional decimal point, is sorted by arithmetic
value. The -n option implies the -b option, but only when at
least one sort-field is specified on the command line.
-r Reverse the current collating sequence.
Field Specification Options
-tc Use c as the word delimiter character; unlike white-space charac-
ters, adjacent delimiters indicate word breaks; if : is the delim-
iter character, :: delimits an empty word.
This is a combination of options that specifies a field, within
each line, to sort on. A sort-field specification can take either
of the following forms:
where sw is the number of the starting word (beginning with `0')
to include in the field, ew is the number of the word before which
to end the field, and cf is a string containing collating flags
(without a leading `-'.) When included in a sort-field specifica-
tion, these flags apply only to the field being specified, and
when given, override other collating flags given in separate argu-
ments (which otherwise apply to an entire line).
If the -ew option is omitted, the field continues to the end of a
You can apply a character offset to sw and ew to indicate that a
field is to start or end a given number of characters within a
word, using the notation: `w.c'. A starting position specified in
the form: `+w.c' indicates the character in position c (beginning
with 0 for the first character), within word w (1 and 1.0 are
equivalent). An ending position specified in the form: `-w.c'
indicates that the field ends at the character just prior to posi-
tion c (beginning with 0 for the delimiter just prior to the first
character), within word w. If the -b flag is in effect, c is
counted from the first non-white-space or non-delimiter character
in the field, otherwise, delimiter characters are counted.
-c Check that the input file is sorted according to the ordering
rules; give no output unless the file is out of sort.
-m Merge only, the input files are already sorted.
-u Unique. Emit only the first line in each set of lines for which
all sorting fields compare equally.
Direct output to the file specified as output-file, instead of
the standard output. This file may be the same as one of the
The amount of main memory used by the sort has a large impact on
its performance. Sorting a small file in a large amount of mem-
ory is a waste. If this option is omitted, sort begins using a
system default memory size, and continues to add space as
needed. If this option is given sort starts with kmem, kilo-
bytes of memory, if allowed, or as close to that amount as pos-
sible. Supplying -y0 guarantees that sort starts with a minimum
of memory. By convention, -y (with no argument) starts with
The size of the longest line read is recorded in the sort phase
so that buffers can be allocated during the merge phase. If the
sort phase is omitted because either of the -c or -m options is
in effect, a default size of 1024 bytes is used. Lines longer
than the buffer size terminate sort abnormally. Supplying the
actual number of bytes in the longest line to be merged (or some
larger value) avoids this.
The directory argument is the name of a directory in which to
place temporary files.
Sort the contents of input-file with word number 1 (the second word) as
the sort key:
sort +1 -2 input-file
Sort, in reverse order, the contents of input-file1 and input-file2,
placing the output in output-file and using the first character of the
second field as the sort key:
sort -r -o output-file +1.0 -1.1 input-file1 input-file2
Sort, in reverse order, the contents of input-file1 and input-file2
using the first non-blank character of the second field as the sort
sort -r +1.0b -1.1b input-file1 input-file2
Print the password file (passwd(5)) sorted by the numeric user ID (the
third colon-separated field):
sort -t: +2n -3 /etc/passwd
Print the lines of the already sorted file input-file, suppressing all
but the first occurrence of lines having the same third field (the
options -mu with just one input file make the choice of a unique repre-
sentative from a set of equal lines predictable):
sort -mu +2 -3 input-file
comm(1), join(1), rev(1), uniq(1)
Comments and exits with non-zero status for various trouble conditions
(such as when input lines are too long), and for disorders discovered
under the -c option.
When the last line of an input file is missing a NEWLINE, sort appends
one, prints a warning message, and continues.
19 September 1989 SORT(1V)