sort - sort and/or merge files
sort [-cmu] [-ooutput] [-ykmem] [-zrecsz] [-bdfiMnr] [-tx]
[-kkeydef] [+pos1 [-pos2]] [-T tdir] [files]
The sort command sorts lines of all the named files together and writes
the result on the standard output. The standard input is read if - is
used as a filename or no input files are named.
Comparisons are based on one or more sort keys extracted from each line
of input. By default, there is one sort key, the entire input line, and
ordering is lexicographic by bytes in machine collating sequence.
sort processes supplementary code set characters according to the locale
specified in the LC_CTYPE and LC_COLLATE environment variables [see LANG
on environ(5)], except as noted below. Supplementary code set characters
are collated in code order.
The following options alter the default behavior:
-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 assumed to be already sorted.
-u Unique: suppress all but one in each set of lines having equal keys.
If used with -c, check that there are no lines with duplicate keys,
in addition to checking that the input file is sorted.
The argument given is the name of an output file to use instead of
the standard output. This file may be the same as one of the
The amount of main memory used by sort has a large impact on its
performance. Sorting a small file in a large amount of memory is a
waste. If this option is omitted, sort begins using a system
default memory size (64Kb), and continues to use more space as
needed, up to a maximum equal to one eighth of physical memory. If
this option is presented with a value (kmem), sort will start using
that number of kilobytes of memory, unless the administrative
minimum or maximum is violated, in which case the corresponding
extremum will be used. sort will continue to allocate memory until
a limit is reached. This limit is capped at 2Gb and computed to be
the larger of the value given to the -y option and one half of
physical memory. Thus, -y0 is guaranteed to start with minimum
memory. By convention, -y (with no argument) starts with maximum
memory; on SGI systems, -y (with no argument) starts with half of
the total physical memory on the system (and the limit on memory
usage is set to one half of physical memory as well).
The size of the longest line read is recorded in the sort phase so
buffers can be allocated during the merge phase. If the sort phase
is omitted via the -c or -m options, a popular system default size
will be used. Lines longer than the buffer size will cause sort to
terminate abnormally. Supplying the actual number of bytes in the
longest line to be merged (or some larger value) will prevent
abnormal termination. If the sort phase is not omitted, then the
maximum line size is calculated and used as the recsz, overriding
the value of -z. Thus, the -z option is significant only when used
with -c or -m.
The argument given is used as a directory where any temporary files
required will be created.
Sort keys can be specified using the options:
Defines a key field that begins at field_start and ends at field_end
inclusive(provided that field_end does not precede field_start). A
missing field_end means the end of the line. A field comprises a
maximal sequence of non-separating characters and, in the absence of
option -t , any preceding field separator.
The field_start portion of the argument has the form:
Fields and characters within fields are numbered starting with 1. The
field_number and first_character pieces, interpreted as positive decimal
integers, specify the first character to be used as part of a sort key.
If first_character is omitted, it refers to the first character of the
The field_end portion of the argument has the form:
The field_number is as described above for field_start. The
last_character piece, interpreted as a non-negative decimal integer,
specifies the last character to be used as part of the sort key. If
last_character evaluates to zero or last_character is omitted, it refers
to the last character of the field specified by field_number.
type is a modifier from the list of characters bdfinr. Each modifier
behaves like the corresponding options(see below), but apply only to the
key field to which they are attached.
This is an obsolescent form of -k. pos1 corresponds to field_start,
pos2 corresponds to field_end, except that both fields and
characters are numbered from zero instead of one. The optional type
modifiers are the same as in -k option.
The following options override the default ordering rules.
-d ``Dictionary'' order: only letters, digits, and blanks (spaces and
tabs) are significant in comparisons. No comparison is performed
for multibyte characters.
-f Fold lowercase letters into uppercase for the purpose of comparison.
Does not apply to multibyte characters.
-i Ignore non-printable characters. Multibyte and embedded NULL
characters are also ignored.
-M Compare as months. The first three non-blank characters of the
field are folded to uppercase and compared. Month names are
processed according to the locale specified in the LC_TIME
environment variable [see LANG on environ(5)]. For example, in an
English locale the sorting order would be ``JAN'' < ``FEB'' < . . .
< ``DEC.'' Invalid fields compare low to ``JAN.'' The -M option
implies the -b option (see below).
-n An initial numeric string, consisting of optional blanks, an
optional minus sign, and zero or more digits with an optional
decimal point, is sorted by arithmetic value. An empty digit string
is treated as zero. Leading zeros and signs on zeros do not affect
ordering. The -n option implies the -b option (see below).
-r Reverse the sense of comparisons.
-b Ignore leading blanks when determining the starting and ending
positions of a restricted sort key.
-tx Use x as the field separator character; x is not considered to be
part of a field (although it may be included in a sort key). Each
occurrence of x is significant (for example, xx delimits an empty
field). x may be a supplementary code set character. The default
field separators are blank characters.
When ordering options appear before restricted sort key specifications,
the requested ordering rules are applied globally to all sort keys. When
attached to a specific sort key (described below), the specified ordering
options override all global ordering options for that key.
When there are multiple sort keys, later keys are compared only after all
earlier keys compare equal. Except when the -u option is specified, lines
that otherwise compare equal are ordered as if none of the options -d,
-f, -i, -n or -k were present (but with -r still in effect, if it was
specified) and with all bytes in the lines significant to the comparison.
Sort the contents of infile with the second field as the sort key:
sort -k2 infile
Sort, in reverse order, the contents of infile1 and infile2, placing the
output in outfile and using the first character of the second field as
the sort key:
sort -r -o outfile <b>-k 2.1,2.1 infile1 infile2
Sort, in reverse order, the contents of infile1 and infile2 using the
first non-blank character of the second field as the sort key:
sort -r +1.0b -1.1b infile1 infile2
Print the password file [passwd(4)] sorted by the numeric user ID (the
third colon-separated field):
sort -t: +2n -3 /etc/passwd
Sort the contents of the password file using the group ID (third field)
as the primary sort key and the user ID (second field) as the secondary
sort -t: +3 -4 +2 -3 /etc/passwd
Print the lines of the already sorted file infile, suppressing all but
the first occurrence of lines having the same third field (the options
-um with just one input file make the choice of a unique representative
from a set of equal lines predictable):
sort -um +2 -3 infile
language-specific message file [See LANG on environ (5).]
comm(1), join(1), uniq(1)
Comments and exits with non-zero status for various trouble conditions
(for example, when input lines are too long), and for disorder discovered
under the -c option. When the last line of an input file is missing a
newline character, sort appends one, prints a warning message, and
continues. sort does not guarantee preservation of relative line
ordering on equal keys.
If the -i and -f options are both used, or if the -i and -d are both
used, the last one given controls the sort behavior; it is not currently
possible to sort with folded case or dictionary order and non-printing
characters ignored, because of the method used to implement these
PPPPaaaaggggeeee 5555 [ Back ]