1 .\" $FreeBSD: src/gnu/usr.bin/sort/sort.1,v 1.5.8.1 2001/07/22 11:01:34 dd Exp $
2 .\" $DragonFly: src/gnu/usr.bin/sort/sort.1,v 1.2 2003/06/17 06:25:49 dillon Exp $
3 .TH SORT 1 "GNU Text Utilities" "FSF" \" -*- nroff -*-
5 sort \- sort lines of text files
8 [\-cmus] [\-t separator] [\-o output-file] [\-T tempdir] [\-bdfiMnr]
9 [+POS1 [\-POS2]] [\-k POS1[,POS2]] [file...]
12 {\-\-help,\-\-version}
15 documents the GNU version of
18 sorts, merges, or compares all the lines from the given files, or the standard
19 input if no files are given. A file name of `-' means standard input.
22 writes the results to the standard output.
25 has three modes of operation: sort (the default), merge, and check for
26 sortedness. The following options change the operation mode:
29 Check whether the given files are already sorted: if they are not all
30 sorted, print an error message and exit with a status of 1.
33 Merge the given files by sorting them as a group. Each input file
34 should already be individually sorted. It always works to sort
35 instead of merge; merging is provided because it is faster, in the
38 A pair of lines is compared as follows:
39 if any key fields have been specified,
41 compares each pair of fields, in the order specified on the command
42 line, according to the associated ordering options, until a difference
43 is found or no fields are left.
45 If any of the global options
47 are given but no key fields are
50 compares the entire lines according to the global options.
52 Finally, as a last resort when all keys compare equal
53 (or if no ordering options were specified at all),
55 compares the lines byte by byte in machine collating sequence.
56 The last resort comparison honors the
61 (stable) option disables this last-resort comparison so that
62 lines in which all fields compare equal are left in their original
63 relative order. If no fields or global options are specified,
69 has no limits on input line length or restrictions on bytes allowed
70 within lines. In addition, if the final byte of an input file is not
73 silently supplies one.
75 If the environment variable
79 uses it as the directory in which to put temporary files instead of
80 the default, /tmp. The
82 option is another way to select the directory for temporary files; it
83 overrides the environment variable.
85 The following options affect the ordering of output lines. They may
86 be specified globally or as part of a specific key field. If no key
87 fields are specified, global options apply to comparison of entire
88 lines; otherwise the global options are inherited by key fields that
89 do not specify any special options of their own.
92 Ignore leading blanks when finding sort keys in each line.
95 Sort in `phone directory' order: ignore all characters except letters,
96 digits and blanks when sorting.
99 Fold lower case characters into the equivalent upper case characters
100 when sorting so that, for example, `b' is sorted the same way `B' is.
103 Ignore characters outside the ASCII range 040-0176 octal (inclusive)
107 An initial string, consisting of any amount of white space, followed
108 by three letters abbreviating a month name, is folded to UPPER case
109 and compared in the order `JAN' < `FEB' < ... < `DEC.' Invalid names
110 compare low to valid names.
113 Compare according to arithmetic value an initial numeric string
114 consisting of optional white space, an optional \- sign, and zero or
115 more digits, optionally followed by a decimal point and zero or more
119 Reverse the result of comparison, so that lines with greater key
120 values appear earlier in the output instead of later.
127 instead of to the standard output. If
129 is one of the input files,
131 copies it to a temporary file before sorting and writing the output to
137 as the field separator when finding the sort keys in each line. By
138 default, fields are separated by the empty string between a
139 non-whitespace character and a whitespace character. That is to say,
140 given the input line ` foo bar',
142 breaks it into fields ` foo' and ` bar'. The field separator is not
143 considered to be part of either the field preceding or the field
147 For the default case or the
149 option, only output the first of a sequence of lines that compare
152 option, check that no pair of consecutive lines compares equal.
155 Specify a field within each line to use as a sorting key. The field
156 consists of the portion of the line starting at POS1 and up to (but
157 not including) POS2 (or to the end of the line if POS2 is not given).
158 The fields and character positions are numbered starting with 0.
161 An alternate syntax for specifying sorting keys.
162 The fields and character positions are numbered starting with 1.
164 A position has the form \fIf\fP.\fIc\fP, where \fIf\fP is the number
165 of the field to use and \fIc\fP is the number of the first character
166 from the beginning of the field (for \fI+pos\fP) or from the end of
167 the previous field (for \fI\-pos\fP). The .\fIc\fP part of a position
168 may be omitted in which case it is taken to be the first character in
171 option has been given, the .\fIc\fP part of a field specification is
172 counted from the first nonblank character of the field (for
173 \fI+pos\fP) or from the first nonblank character following the
174 previous field (for \fI\-pos\fP).
176 A \fI+pos\fP or \fI-pos\fP argument may also have any of the option
179 appended to it, in which case the global ordering options are not used
180 for that particular field. The
182 option may be independently attached to either or both of the
183 \fI+pos\fP and \fI\-pos\fP parts of a field specification, and if it
184 is inherited from the global options it will be attached to both.
189 option is used, thus implying a
193 option is taken to apply to both the \fI+pos\fP and the \fI\-pos\fP
194 parts of a key specification. Keys may span multiple fields.
196 In addition, when GNU
198 is invoked with exactly one argument, the following options are recognized:
201 Print a usage message on standard output and exit successfully.
204 Print version information on standard output then exit successfully.
206 Historical (BSD and System V) implementations of
208 have differed in their interpretation of some options,
214 GNU sort follows the POSIX behavior, which is
215 usually (but not always!) like the System V behavior.
222 has been changed in the same way.
223 This may affect the meaning of character positions in field
224 specifications in obscure cases.
225 If this bites you the fix is to add an explicit
228 The different meaning of field numbers depending
231 is used is confusing.
232 It's all POSIX's fault!