sort(1) CLIX sort(1)
NAME
sort - Sorts and/or merges files
SYNOPSIS
sort [-cmu] [-ooutput] [-ykmem] [-zrecsz] [-dfiMnr] [-btx] [+pos1 [-pos2]]
[file ... ]
FLAGS
-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: suppress all but one in each set of lines having equal
keys.
-ooutput The argument given is the name of an output file to use instead
of stdout. This file may be the same as one of the inputs.
There may be optional blanks between -o and output.
-ykmem 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
memory is a waste. If this flag is omitted, the sort command
begins using a system default memory size, and continues to use
more space as needed. If this flag is presented with a value,
kmem, the sort command 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. Thus, -y0 is guaranteed to start with minimum
memory. By convention, -y (with no argument) starts with
maximum memory.
-zrecsz 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 with the -c or -m flag, a popular system
default size will be used. Lines longer than the buffer size
will cause the sort command to terminate abnormally. Supplying
the actual number of bytes in the longest line to be merged (or
some larger value) will prevent abnormal termination.
Overriding Ordering Rules
The following flags override the default ordering rules.
-d ``Dictionary'' order: only letters, digits, and blanks (spaces and
tabs) are significant in comparisons.
-f Fold lowercase letters into uppercase.
2/94 - Intergraph Corporation 1
sort(1) CLIX sort(1)
-i Ignore nondisplayable characters.
-M Compare as months. The first three nonblank characters of the field
are folded to uppercase and compared. For example, in English the
sorting order is ``JAN'' < ``FEB'' < ... < ``DEC''. Fields that
compare entries other than month abbreviations, such as low to
``JAN'', are not valid. The -M flag implies the -b flag.
-n An initial numeric string, consisting of optional blanks, optional
minus sign, and zero or more digits with optional decimal point, is
sorted by arithmetic value. The -n flag implies the -b flag. Note
that the -b flag is only effective when restricted sort key
specifications are in effect.
-r Reverse the sense of comparisons.
Overriding Field Separator Defaults
The treatment of field separators can be altered using the following
flags:
-b Ignore leading blanks when determining the starting and ending
positions of a restricted sort key. If the -b flag is specified
before the first +pos1 argument, it will be applied to all +pos1
arguments. Otherwise, the b flag may be attached independently to
each +pos1 or -pos2 argument (see below).
-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).
DESCRIPTION
The sort command sorts lines of all the named files together and writes
the result on stdout. stdin 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 (see
FLAGS for information on changing defaults).
When ordering flags 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
flags override all global ordering flags for that key.
The notation +pos1 -pos2 restricts a sort key to one beginning at pos1 and
ending just before pos2. The characters at position pos1 and just before
pos2 are included in the sort key (provided that pos2 does not precede
2 Intergraph Corporation - 2/94
sort(1) CLIX sort(1)
pos1).
A missing -pos2 means the end of the line.
Specifying pos1 and pos2 involves the notion of a field, a minimal
sequence of characters followed by a field separator or a newline. By
default, the first blank (space or tab) of a sequence of blanks acts as
the field separator. All blanks in a sequence of blanks are considered to
be part of the next field; for example, all blanks at the beginning of a
line are considered to be part of the first field. Default field
separators can be altered with the -b and -tx flags. The
The pos1 and pos2 arguments each have the form m.n optionally followed by
one or more of the flags bdfinr. A starting position specified by +m.n is
interpreted to mean the n+1st character in the m+1st field. A missing .n
means .0, indicating the first character of the m+1st field. If the b
flag is in effect, n is counted from the first nonblank in the m+1st
field; +m.0b refers to the first nonblank character in the m+1st field.
A last position specified by -m.n is interpreted to mean the nth character
(including separators) after the last character of the mth field. A
missing .n means .0, indicating the last character of the mth field. If
the b flag is in effect n is counted from the last leading blank in the
m+1st field; -m.1b refers to the first nonblank in the m+1st field.
When there are multiple sort keys, later keys are compared only after all
earlier keys compare equal. Lines that otherwise compare equal are
ordered with all bytes significant.
EXAMPLES
1. To sort the contents of infile with the second field as the sort key:
sort +1 -2 infile
2. To 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 +1.0 -1.2 infile1 infile2
3. Sort, in reverse order, the contents of infile1 and infile2 using the
first nonblank character of the second field as the sort key:
sort -r +1.0b -1.1b infile1 infile2
4. Display the password file (passwd) sorted by the numeric user ID (the
third colon-separated field):
2/94 - Intergraph Corporation 3
sort(1) CLIX sort(1)
sort -t: +2n -3 /etc/passwd
5. Display the lines of the already sorted file infile, suppressing all
but the first occurrence of lines having the same third field (the
flags -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
FILES
/usr/tmp/stm???
CAUTIONS
Comments and exits with nonzero status for various trouble conditions (for
example, when input lines are too long), and for disorder discovered under
the -c flag. When the last line of an input file is missing a newline
character, sort appends one, displays a warning message, and continues.
The sort command does not guarantee preservation of relative line ordering
on equal keys.
EXIT VALUES
The sort command exits with a nonzero value if it fails.
RELATED INFORMATION
Commands: comm(1), join(1), uniq(1)
4 Intergraph Corporation - 2/94