1 .\" $NetBSD: sort.1,v 1.34 2013/05/29 15:00:35 wiz Exp $
3 .\" Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Ben Harris and Jaromir Dolecek.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
30 .\" Copyright (c) 1991, 1993
31 .\" The Regents of the University of California. All rights reserved.
33 .\" This code is derived from software contributed to Berkeley by
34 .\" the Institute of Electrical and Electronics Engineers, Inc.
36 .\" Redistribution and use in source and binary forms, with or without
37 .\" modification, are permitted provided that the following conditions
39 .\" 1. Redistributions of source code must retain the above copyright
40 .\" notice, this list of conditions and the following disclaimer.
41 .\" 2. Redistributions in binary form must reproduce the above copyright
42 .\" notice, this list of conditions and the following disclaimer in the
43 .\" documentation and/or other materials provided with the distribution.
44 .\" 3. Neither the name of the University nor the names of its contributors
45 .\" may be used to endorse or promote products derived from this software
46 .\" without specific prior written permission.
48 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 .\" @(#)sort.1 8.1 (Berkeley) 6/6/93
67 .Nd sort or merge text files
73 .Ar field1 Ns Op Li \&, Ns Ar field2
83 utility sorts text files by lines.
84 Comparisons are based on one or more sort keys extracted
85 from each line of input, and are performed lexicographically.
86 By default, if keys are not given,
88 regards each input line as a single field.
90 The following options are available:
93 Check that the single input file is sorted.
94 If the file is not sorted,
96 produces the appropriate error messages and exits with code 1; otherwise,
105 Ignored for compatibility with earlier versions of
108 Merge only; the input files are assumed to be pre-sorted.
110 The argument given is the name of an
112 file to be used instead of the standard output.
113 This file can be the same as one of the input files.
115 Don't use stable sort.
116 Default is to use stable sort.
118 Use stable sort, keeps records with equal keys in their original order.
120 Provided for compatibility with other
122 implementations only.
126 as the directory for temporary files.
127 The default is the value specified in the environment variable
134 Unique: suppress all but one in each set of lines having equal keys.
137 option, check that there are no lines with duplicate keys.
140 The following options override the default ordering rules.
141 When ordering options appear independent of key field
142 specifications, the requested field ordering rules are
143 applied globally to all sort keys.
144 When attached to a specific key (see
146 the ordering options override
147 all global ordering options for that key.
150 Only blank space and alphanumeric characters
152 .\" to the current setting of LC_CTYPE
154 in making comparisons.
156 Considers all lowercase characters that have uppercase
157 equivalents to be the same for purposes of comparison.
159 Ignore all non-printable characters.
161 Sort by the string length of the field, not by the field itself.
163 An initial numeric string, consisting of optional blank space, optional
164 plus or minus sign, and zero or more digits (including decimal point)
166 .\" optional radix character and thousands
168 .\" (as defined in the current locale),
169 is sorted by arithmetic value.
172 option no longer implies the
176 Reverse the sense of comparisons.
179 The treatment of field separators can be altered using these options:
182 Ignores leading blank space when determining the start
183 and end of a restricted sort key.
186 option specified before the first
188 option applies globally to all
193 option can be attached independently to each
200 option has no effect unless key fields are specified.
203 is used as the field separator character.
206 is not considered to be part of a field when determining
207 key offsets (see below).
210 is significant (for example,
212 delimits an empty field).
215 is not specified, the default field separator is a sequence of
216 blank-space characters, and consecutive blank spaces do
218 delimit an empty field; further, the initial blank space
220 considered part of a field when determining key offsets.
223 is used as the record separator character.
224 This should be used with discretion;
225 .Fl R Aq Ar alphanumeric
226 usually produces undesirable results.
227 The default record separator is newline.
228 .It Fl k Ar field1 Ns Op Li \&, Ns Ar field2
229 Designates the starting position,
231 and optional ending position,
236 option replaces the obsolescent options
242 The following operands are available:
245 The pathname of a file to be sorted, merged, or checked.
248 operands are specified, or if
253 the standard input is used.
256 A field is defined as a minimal sequence of characters followed by a
257 field separator or a newline character.
258 By default, the first
259 blank space of a sequence of blank spaces acts as the field separator.
260 All blank spaces in a sequence of blank spaces are considered
261 as part of the next field; for example, all blank spaces at
262 the beginning of a line are considered to be part of the
268 .Ar field1 Ns Op \&, Ns Ar field2
272 argument defaults to the end of a line.
279 .Ar m Ns Li \&. Ns Ar n
280 and can be followed by one or more of the letters
285 which correspond to the options discussed above.
288 position specified by
289 .Ar m Ns Li \&. Ns Ar n
290 .Pq Ar m , n No \*[Gt] 0
291 is interpreted as the
302 indicating the first character of the
308 is counted from the first non-blank character in the
312 refers to the first non-blank character in the
318 position specified by
319 .Ar m Ns Li \&. Ns Ar n
323 character (including separators) of the
328 indicates the last character of the
333 designates the end of a line.
338 .Ar v Li \&. Ar x Li \&,
342 is synonymous with the obsolescent option
344 .Cm \(pl Ar v-\&1 Li \&. Ar x-\&1
345 .Fl Ar w-\&1 Li \&. Ar y ;
352 .Ar v Li \&. Ar x Li \&, Ar w
356 .Cm \(pl Ar v-\&1 Li \&. Ar x-\&1
362 option is still supported, except for
363 .Fl Ns Ar w Ns Li \&.0b ,
368 If the following environment variable exists, it is used by
373 uses the contents of the
375 environment variable as the path in which to store
379 .Bl -tag -width outputNUMBER+some -compact
381 Default temporary files.
382 .It Ar output Ns NUMBER
383 Temporary file which is used for output if
386 Once sorting is finished, this file replaces
394 Sort exits with one of the following values:
395 .Bl -tag -width flag -compact
399 On disorder (or non-uniqueness) with the
418 implementation appeared in
423 Posix requires the locale's thousands separator be ignored in numbers.
424 It may be faster to sort very large files in pieces and then explicitly
429 has no limits on input line length (other than imposed by available
430 memory) or any restrictions on bytes allowed within lines.
439 and thus fails on protected directories.
441 Input files should be text files.
442 If file doesn't end with record separator (which is typically newline), the
444 utility silently supplies one.
448 uses lexicographic radix sorting, which requires
449 that sort keys be kept in memory (as opposed to previous versions which used quick
450 and merge sorts and did not.)
451 Thus performance depends highly on efficient choice of sort keys, and the
457 option should be used whenever possible.
464 and may take twice as long.