.TH sort "" "" Command
.PC "Sort lines of text"
\fBsort [\-bcdfimnru] [\-t \fIc\^\fB] [\-o \fIoutfile\^\fB] [\-T \fIdir\^\fB] [+\fIbeg\^\fB[\-\fIend\^\fB]][\fIfile .\^.\^.\fB]\fR
.PP
.HS
.SH Options:
.IC \fB\-c\fR
Check if input is already ordered
.IC \fB\-m\fR
Merge already-sorted input files
.IC "\fB\-o\fI name\fR"
Place output in \fIname\fR, not stdout
.IC \fB\-t\fIc\fR
Tab character is \fIc\fR
.IC "\fB\-T\fI dir\fR"
Use \fIdir\fR for temporary files
.IC \fB\-u\fR
Output only records with unique keys
.SH "Key options:"
.IC \fB\-b\fR
Skip leading blanks in fields
.IC \fB\-d\fR
Dictionary ordering for keys
.IC \fB\-f\fR
Fold upper case into lower case in key comparison
.IC \fB\-i\fR
Ignore control characters in key comparison
.IC \fB\-n\fR
Numeric comparison
.IC \fB\-r\fR
Reverse sort ordering
.SH Position:
.IC \fB+\fIm\fB.\fInf\fR
Key starts \fIm\fR fields into record and \fIn\fR characters into that field;
\fIf\fR may contain optional flags from
key options above which apply only to that positional
.IC \fB\-\fIm\fB.\fInf\fR
Optional ending position of key (same form as above)
.Pp
If no \fIfile\fR is given, sort stdin.
.HE
.B sort
reads lines from each
.IR file ,
or from the standard input if no file is specified.
It sorts what it reads, and writes the sorted material to the standard output.
.PP
.B sort
sorts lines by comparing a
.I key
from each line.
By default, the key is the entire input line (or \fIrecord\^\fR)
and ordering is in ASCII order.
The key, however, can be one or more \fIfields\fR within the input record;
by using the appropriate options, you can select which fields are used as
the key, and dictate the character that is used to separate the fields.
.PP
The following options affect how the key is constructed
or how the output is ordered.
.IP "\fB\-b\fR" 0.3i
Ignore leading white space (blanks or tabs)
in key comparisons.
.IP "\fB\-d\fR"
Dictionary ordering:
use only letters, blanks, and digits when comparing keys.
This is essentially the ordering used to sort telephone directories.
.IP "\fB\-f\fR"
Fold upper-case letters to lower case for comparison purposes.
.IP "\fB\-i\fR"
Ignore
all characters outside of the printable ASCII range (octal 040\-0176).
.IP "\fB\-n\fR"
The key is a numeric string
that consists of optional leading blanks and optional minus
sign followed by any number of digits with an optional decimal point.
Ordering is by the numeric, as opposed to alphabetic, value of the string.
.IP "\fB\-r\fR"
Reverse the ordering, i.e.,
.B sort
from largest to smallest.
.PP
As noted above, the key compared from each line
need not be the entire input line.
The option
.BI + beg
indicates the beginning position of the key field in the input line,
and the optional
\fB\-\fIend\fR
indicates that the key field ends just before the
.I end
position.
If no
\fB\-\fIend\fR
is given,
the key field ends at the end of the line.
Each of these positional indicators has the form
.BI + m.nf
or
.BI \- m.nf,
where
.I m
is the number of fields to skip in the input line and
.I n
is the number of characters to skip after skipping fields.
Optional flags
.I f
are chosen from the above key flags
(\fBbdfinr\fR) and are local to the specified field.
.PP
The following additional options control how
.B sort
works.
.IP "\fB\-c\fR" 0.3i
Check the input to see if it is sorted.
Print the first out-of-order line found.
.IP "\fB\-m\fR"
Merge the input files.
.B sort
assumes each
.I file
to be sorted already.
With large files,
.B sort
runs much faster with this option.
.IP "\fB\-o \fIoutfile\fR"
Put the output into
.I outfile
rather than on the standard output.
This allows
.B sort
to work correctly if the output file is one of the input files.
.IP "\fB\-t\fIc\fR"
Use the character
.I c
to separate fields rather than the default blanks and tabs.
For example, \fB\-t/\fR uses the slash instead of white space to separate
fields; this is useful when sorting file names and directory names.
.IP "\fB\-T \fIdir\fR"
Create temporary files in directory
.I dir
rather than the standard place.
.IP "\fB\-u\fR"
Suppress multiple copies of lines with key fields that compare equally.
.PP
The following example sorts the password file
.B /etc/passwd,
first by group number (field 4) and then by user name (field 1):
.DM
	sort \-t: +3n \-4 +0 \-1 /etc/passwd
.DE
.SH Limits
The \*(CO implementation of
.B sort
sets the following limits on input and output:
.DS
.ta 0.5i 3.0i
	Characters per input record	399
	Characters per output record	399
	Characters per field	399
.DE
.SH Files
\fB/usr/tmp/sort*\fR \(em First attempt at temporary files
.br
\fB/tmp/sort*\fR \(em Second attempt at temporary files
.SH "See Also"
.Xr "ASCII," ascii
.Xr "commands," commands
.Xr "ctype.h," ctype.h
.Xr "qsort()," qsort
.Xr "shellsort()," shellsort
.Xr "tsort," tsort
.Xr "uniq" uniq
.SH Diagnostics
.B sort
returns a nonzero exit status if internal problems occurred,
or if the file was not correctly sorted in the case of the
.B \-c
option.
