.\"
.\" Derived from sort(1), Unix 32V:
.\" Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"   Redistributions of source code and documentation must retain the
.\"    above copyright notice, this list of conditions and the following
.\"    disclaimer.
.\"   Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"   All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"      This product includes software developed or owned by Caldera
.\"      International, Inc.
.\"   Neither the name of Caldera International, Inc. nor the names of
.\"    other contributors may be used to endorse or promote products
.\"    derived from this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
.\" LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
.\" BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
.\" OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
.\" EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.TH SORT 1 "2/2/05" "Heirloom Toolchest" "User Commands"
.SH NAME
sort \- sort or merge files
.SH SYNOPSIS
.HP
.nh
.ad l
\fBsort\fR [\fB\-mubdf\&iMnrt\fIx\fR]
[\fB+\fIpos1\ \fR[\fB\-\fIpos2\fR]] ...
[\fB\-k\ \fIkeydef\fR]
[\fB\-o\ \fIname\fR] [\fB\-T\ \fIdirectory\fR]
[\fB\-y\fR[\fIkmem\fR]] [\fB\-z\ \fIrecsz\fR]
[\fIname\fR]\ ...
.br
.hy 1
.ad b
.SH DESCRIPTION
.I Sort
sorts
lines of all the named files together
and writes the result on
the standard output.
The name `\-' means
the standard input.
If no input files are named, the standard input is sorted.
.PP
The default sort key is an entire line.
Default ordering is
lexicographic by characters in the
current collating sequence.
.PP
The behavior of
.I sort
is altered by the following options:
.TP 5
.B \-c
Check that the input file is sorted according to the ordering rules,
give no output unless the file is out of sort.
Only one input file may be given.
.TP 5
.B  \-m
Merge only, the input files are already sorted.
.TP 5
\fB\-o\ \fIfile\fR
The
.I file
argument is the name of an output file
to use instead of the standard output.
This file may be the same as one of the inputs.
.TP 5
\fB\-T\ \fItmpdir\fR
The
.I tmpdir
argument is the name of a directory in which temporary files
should be made.
.TP 5
.B  \-u
Suppress all but one in each
set of equal lines.
Ignored bytes
and bytes outside keys
do not participate in
this comparison.
.TP 5
\fB\-y\fR[\fIkmem\fR]
Sets the amount of memory to be allocated initially to
.I kmem
(in kilobytes).
If
.I kmem
is omitted or is larger than the maximum allowed value,
a maximum value is used.
If
.I kmem
is 0 or is smaller than the minimum allowed value,
a minimum value is used.
More memory is allocated during the sort phase
if a line exceeds the expected length.
.TP 5
\fB\-z\fI\ recsz\fR
Sets the length of the maximum allowed line during the merge phase to
.IR recsz .
The length of lines is normally computed during the sort phase,
so this option is relevant only if the sort phase is omitted
(with
.B \-c
or
.BR \-m ).
.PP
The ordering is affected globally by the following options,
one or more of which may appear.
.TP 5
.B  \-b
Ignore leading blanks (spaces and tabs) in field comparisons.
.TP 5
.B  \-d
`Dictionary' order: only letters, digits and blanks
are significant in comparisons.
.TP 5
.B  \-f
Fold upper case
letters onto lower case.
.TP 5
.B  \-i
Ignore nonprintable characters in nonnumeric comparisons.
.TP
.B \-M
Compare the first three characters of the field
without regard to case
as abbreviated month names
`JAN', `FEB', `MAR' etc.
Invalid fields compare to `JAN'.
Implies option
.BR \-b .
.TP 5
.B  \-n
An initial numeric string,
consisting of optional blanks, optional minus sign,
and zero or more digits with optional decimal point
and optional thousands separators,
is sorted by arithmetic value.
.\"Option
.\".B \-n
.\"implies option
.\".BR \-b .
.TP 5
.B  \-r
Reverse the sense of comparisons.
.TP 5
.BI \-t x
`Tab character' separating fields is
.IR x .
With
.B /usr/5bin/sort
and
.BI /usr/5bin/s42/sort ,
the
.I x
argument is optional;
if it is missing,
the field separator is reset to the default.
.PP
The notation
.BI + "pos1 " "\-\fIpos2"
restricts a sort key to a field beginning at
.I pos1
and ending just before
.IR pos2 .
.I Pos1
and
.I pos2
each have the form
.IB m . n\fR,
optionally followed by one or more of the flags
.B bdf\&iMnr,
where
.I m
tells a number of fields to skip from the beginning of the line and
.I n
tells a number of characters to skip further.
If any flags are present they override all the global
ordering options for this key.
If the
.B \-b
option is in effect
.I n
is counted from the first nonblank in the field;
.B \-b
is attached independently to 
.IR pos2 .
A missing
\&\fB.\fIn\fR
means .0;
a missing
.BI \- pos2
means the end of the line.
Under the
.BI \-t x
option, fields are strings separated by
.IR x ;
otherwise fields are
nonempty nonblank strings separated by blanks.
.PP
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.
.PP
The
\fB\-k\fI\ keydef\fR
option,
which was introduced by POSIX.2,
provides another method to specify sort keys.
Start field and end field are separated by commas,
field and character numbers start at 1 instead of 0,
and, if no character position is specified for the end key,
the last character of the field is used;
otherwise, the meaning of field specifications is identical.
.SH EXAMPLES
Print in alphabetical order all the unique spellings
in a list of words.
Capitalized words differ from uncapitalized.
.PP
.ti +8
sort \-u +0f +0 list
.PP
Print the password file
.RI ( passwd (5))
sorted by user id number (the 3rd colon-separated field).
.PP
.ti +8
sort \-t: +2n /etc/passwd
.PP
Print the first instance of each month in an already sorted file
of (month day) entries.
The options
.B \-um
with just one input file make the choice of a
unique representative from a set of equal lines predictable.
.PP
.ti +8
sort \-um +0 \-1 dates
.SH "ENVIRONMENT VARIABLES"
.TP
.BR LANG ", " LC_ALL
See
.IR locale (7).
.TP
.B LC_COLLATE
Gives the sort order for comparing strings.
.TP
.B LC_CTYPE
Determines the mapping of bytes to characters
and the character types for the
.BR b ,
.BR d ,
.BR f ,
and
.BR i
field options.
.TP
.B LC_NUMERIC
Affects the radix and thousands separator characters for the
.B \-n
option.
.TP
.B LC_TIME
Sets the month names used with
.BR \-M .
.TP
.B TMPDIR
Specifies a directory to use for temporary files.
.SH FILES
.TP
/var/tmp/stm*, /tmp/*
first and second tries for temporary files
.SH "SEE ALSO"
comm(1),
join(1),
uniq(1),
locale(7)
.SH DIAGNOSTICS
Comments and exits with nonzero status for various trouble
conditions and for disorder discovered under option
.BR \-c .
.SH NOTES
Maximum performance is usually achieved if the
.I TMPDIR
environment variable is set to a directory on a
.I tmpfs
file system,
and the
.I \-y
option is given.
