1 .\" $FreeBSD: src/gnu/usr.bin/sort/sort.1,v 1.5.8.1 2001/07/22 11:01:34 dd Exp $
2 .TH SORT 1 "GNU Text Utilities" "FSF" \" -*- nroff -*-
4 sort \- sort lines of text files
7 [\-cmus] [\-t separator] [\-o output-file] [\-T tempdir] [\-bdfiMnr]
8 [+POS1 [\-POS2]] [\-k POS1[,POS2]] [file...]
11 {\-\-help,\-\-version}
14 documents the GNU version of
17 sorts, merges, or compares all the lines from the given files, or the standard
18 input if no files are given. A file name of `-' means standard input.
21 writes the results to the standard output.
24 has three modes of operation: sort (the default), merge, and check for
25 sortedness. The following options change the operation mode:
28 Check whether the given files are already sorted: if they are not all
29 sorted, print an error message and exit with a status of 1.
32 Merge the given files by sorting them as a group. Each input file
33 should already be individually sorted. It always works to sort
34 instead of merge; merging is provided because it is faster, in the
37 A pair of lines is compared as follows:
38 if any key fields have been specified,
40 compares each pair of fields, in the order specified on the command
41 line, according to the associated ordering options, until a difference
42 is found or no fields are left.
44 If any of the global options
46 are given but no key fields are
49 compares the entire lines according to the global options.
51 Finally, as a last resort when all keys compare equal
52 (or if no ordering options were specified at all),
54 compares the lines byte by byte in machine collating sequence.
55 The last resort comparison honors the
60 (stable) option disables this last-resort comparison so that
61 lines in which all fields compare equal are left in their original
62 relative order. If no fields or global options are specified,
68 has no limits on input line length or restrictions on bytes allowed
69 within lines. In addition, if the final byte of an input file is not
72 silently supplies one.
74 If the environment variable
78 uses it as the directory in which to put temporary files instead of
79 the default, /tmp. The
81 option is another way to select the directory for temporary files; it
82 overrides the environment variable.
84 The following options affect the ordering of output lines. They may
85 be specified globally or as part of a specific key field. If no key
86 fields are specified, global options apply to comparison of entire
87 lines; otherwise the global options are inherited by key fields that
88 do not specify any special options of their own.
91 Ignore leading blanks when finding sort keys in each line.
94 Sort in `phone directory' order: ignore all characters except letters,
95 digits and blanks when sorting.
98 Fold lower case characters into the equivalent upper case characters
99 when sorting so that, for example, `b' is sorted the same way `B' is.
102 Ignore characters outside the ASCII range 040-0176 octal (inclusive)
106 An initial string, consisting of any amount of white space, followed
107 by three letters abbreviating a month name, is folded to UPPER case
108 and compared in the order `JAN' < `FEB' < ... < `DEC.' Invalid names
109 compare low to valid names.
112 Compare according to arithmetic value an initial numeric string
113 consisting of optional white space, an optional \- sign, and zero or
114 more digits, optionally followed by a decimal point and zero or more
118 Reverse the result of comparison, so that lines with greater key
119 values appear earlier in the output instead of later.
126 instead of to the standard output. If
128 is one of the input files,
130 copies it to a temporary file before sorting and writing the output to
136 as the field separator when finding the sort keys in each line. By
137 default, fields are separated by the empty string between a
138 non-whitespace character and a whitespace character. That is to say,
139 given the input line ` foo bar',
141 breaks it into fields ` foo' and ` bar'. The field separator is not
142 considered to be part of either the field preceding or the field
146 For the default case or the
148 option, only output the first of a sequence of lines that compare
151 option, check that no pair of consecutive lines compares equal.
154 Specify a field within each line to use as a sorting key. The field
155 consists of the portion of the line starting at POS1 and up to (but
156 not including) POS2 (or to the end of the line if POS2 is not given).
157 The fields and character positions are numbered starting with 0.
160 An alternate syntax for specifying sorting keys.
161 The fields and character positions are numbered starting with 1.
163 A position has the form \fIf\fP.\fIc\fP, where \fIf\fP is the number
164 of the field to use and \fIc\fP is the number of the first character
165 from the beginning of the field (for \fI+pos\fP) or from the end of
166 the previous field (for \fI\-pos\fP). The .\fIc\fP part of a position
167 may be omitted in which case it is taken to be the first character in
170 option has been given, the .\fIc\fP part of a field specification is
171 counted from the first nonblank character of the field (for
172 \fI+pos\fP) or from the first nonblank character following the
173 previous field (for \fI\-pos\fP).
175 A \fI+pos\fP or \fI-pos\fP argument may also have any of the option
178 appended to it, in which case the global ordering options are not used
179 for that particular field. The
181 option may be independently attached to either or both of the
182 \fI+pos\fP and \fI\-pos\fP parts of a field specification, and if it
183 is inherited from the global options it will be attached to both.
188 option is used, thus implying a
192 option is taken to apply to both the \fI+pos\fP and the \fI\-pos\fP
193 parts of a key specification. Keys may span multiple fields.
195 In addition, when GNU
197 is invoked with exactly one argument, the following options are recognized:
200 Print a usage message on standard output and exit successfully.
203 Print version information on standard output then exit successfully.
205 Historical (BSD and System V) implementations of
207 have differed in their interpretation of some options,
213 GNU sort follows the POSIX behavior, which is
214 usually (but not always!) like the System V behavior.
221 has been changed in the same way.
222 This may affect the meaning of character positions in field
223 specifications in obscure cases.
224 If this bites you the fix is to add an explicit
227 The different meaning of field numbers depending
230 is used is confusing.
231 It's all POSIX's fault!