386BSD 0.1 development
[unix-history] / usr / src / usr.bin / sort / sort.1
CommitLineData
379847a8
WJ
1.TH SORT 1L \" -*- nroff -*-
2.SH NAME
3sort \- sort lines of text files
4.SH SYNOPSIS
5.B sort
6[\-cmus] [\-t separator] [\-o output-file] [\-bdfiMnr] [+POS1 [\-POS2]]
7[\-k POS1[,POS2]] [file...]
8.SH DESCRIPTION
9This manual page
10documents the GNU version of
11.BR sort .
12.B sort
13sorts, merges, or compares all the lines from the given files, or the standard
14input if no files are given. A file name of `-' means standard input.
15By default,
16.B sort
17writes the results to the standard output.
18.PP
19.B sort
20has three modes of operation: sort (the default), merge, and check for
21sortedness. The following options change the operation mode:
22.TP
23.I \-c
24Check whether the given files are already sorted: if they are not all
25sorted, print an error message and exit with a status of 1.
26.TP
27.I \-m
28Merge the given files by sorting them as a group. Each input file
29should already be individually sorted. It always works to sort
30instead of merge; merging is provided because it is faster, in the
31case where it works.
32.PP
33A pair of lines is compared as follows:
34if any key fields have been specified,
35.B sort
36compares each pair of fields, in the order specified on the command
37line, according to the associated ordering options, until a difference
38is found or no fields are left.
39.PP
40If any of the global options
41.I Mbdfinr
42are given but no key fields are
43specified,
44.B sort
45compares the entire lines according to the global options.
46.PP
47Finally, as a last resort when all keys compare equal
48(or if no ordering options were specified at all),
49.B sort
50compares the lines byte by byte in machine collating sequence. The
51.I \-s
52option disables this last resort comparison, producing a stable sort.
53.PP
54GNU
55.B sort
56has no limits on input line length or restrictions on bytes allowed
57within lines. In addition, if the final byte of an input file is not
58a newline, GNU
59.B sort
60silently supplies one. In some cases, such as exactly what the
61.I \-b
62and
63.I \-f
64options do, BSD and System V
65.B sort
66programs produce different output; GNU
67.B sort
68follows the POSIX behavior, which is usually like the System V behavior.
69.PP
70If the environment variable TMPDIR is set,
71.B sort
72uses it as the directory in which to put temporary files instead of
73the default, /tmp.
74.PP
75The following options affect the ordering of output lines. They may
76be specified globally or as part of a specific key field. If no key
77fields are specified, global options apply to comparison of entire
78lines; otherwise the global options are inherited by key fields that
79do not specify any special options of their own.
80.TP
81.I \-b
82Ignore leading blanks when finding sort keys in each line.
83.TP
84.I \-d
85Sort in `dictionary order': ignore all characters except letters,
86digits and blanks when sorting.
87.TP
88.I \-f
89Fold lower case characters into the equivalent upper case characters
90when sorting so that, for example, `b' is sorted the same way `B' is.
91.TP
92.I \-i
93Ignore characters outside the ASCII range 040-0176 (inclusive) when sorting.
94.TP
95.I \-M
96An initial string, consisting of any amount of white space, followed
97by three letters abbreviating a month name, is folded to lower case
98and compared in the order `jan' < `feb' < ... < `dec.' Invalid names
99compare low to valid names. This option implies
100.IR \-b .
101.TP
102.I \-n
103Compare according to arithmetic value an initial numeric string
104consisting of optional white space, an optional \- sign, and zero or
105more digits, optionally followed by a decimal point and zero or more
106digits. This option implies
107.IR \-b .
108.TP
109.I \-r
110Reverse the result of comparison, so that lines with greater key
111values appear earlier in the output instead of later.
112.PP
113Other options are:
114.TP
115.I "\-o output-file"
116Write output to
117.I output-file
118instead of to the standard output. If
119.I output-file
120is one of the input files,
121.B sort
122copies it to a temporary file before sorting and writing the output to
123.IR output-file .
124.TP
125.I "\-t separator"
126Use character
127.I separator
128as the field separator when finding the sort keys in each line. By
129default, fields are separated by the empty string between a
130non-whitespace character and a whitespace character. That is to say,
131given the input line ` foo bar',
132.B sort
133breaks it into fields ` foo' and ` bar'. The field separator is not
134considered to be part of either the field preceding or the field
135following it.
136.TP
137.I \-u
138For the default case or the
139.I \-m
140option, only output the first of a sequence of lines that compare
141equal. For the
142.I \-c
143option, check that no pair of consecutive lines compares equal.
144.TP
145.I "+POS1 [\-POS2]"
146Specify a field within each line to use as a sorting key. The field
147consists of the portion of the line starting at POS1 and up to (but
148not including) POS2 (or to the end of the line if POS2 is not given).
149The fields and character positions are numbered starting with 0.
150.TP
151.I "\-k POS1[,POS2]"
152An alternate syntax for specifying sorting keys.
153The fields and character positions are numbered starting with 1.
154.PP
155A position has the form \fIf\fP.\fIc\fP, where \fIf\fP is the number
156of the field to use and \fIc\fP is the number of the first character
157from the beginning of the field (for \fI+pos\fP) or from the end of
158the previous field (for \fI\-pos\fP). The .\fIc\fP part of a position
159may be omitted in which case it is taken to be the first character in
160the field. If the
161.I \-b
162option has been given, the .\fIc\fP part of a field specification is
163counted from the first nonblank character of the field (for
164\fI+pos\fP) or from the first nonblank character following the
165previous field (for \fI\-pos\fP).
166.PP
167A \fI+pos\fP or \fI-pos\fP argument may also have any of the option
168letters
169.I Mbdfinr
170appended to it, in which case the global ordering options are not used
171for that particular field. The
172.I \-b
173option may be independently attached to either or both of the
174\fI+pos\fP and \fI\-pos\fP parts of a field specification, and if it
175is inherited from the global options it will be attached to both.
176If a
177.I \-n
178or
179.I \-M
180option is used, thus implying a
181.I \-b
182option, the
183.I \-b
184option is taken to apply to both the \fI+pos\fP and the \fI\-pos\fP
185parts of a key specification. Keys may span multiple fields.