9070 Remove wanboot from gate
[unleashed.git] / usr / src / man / man1 / sort.1
blobe270b4efdce38879801f96fdb52c7e98e8c8f101
1 '\" te
2 .\" Copyright 1989 AT&T
3 .\" Copyright (c) 2001, Sun Microsystems, Inc.  All Rights Reserved
4 .\" Portions Copyright (c) 1992, X/Open Company Limited  All Rights Reserved
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
6 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
8 .\"  This notice shall appear on any product containing this material.
9 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
10 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
11 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
12 .TH SORT 1 "Nov 19, 2001"
13 .SH NAME
14 sort \- sort, merge, or sequence check text files
15 .SH SYNOPSIS
16 .LP
17 .nf
18 \fB/usr/bin/sort\fR [\fB-bcdfimMnru\fR] [\fB-k\fR \fIkeydef\fR] [\fB-o\fR \fIoutput\fR]
19      [\fB-S\fR \fIkmem\fR] [\fB-t\fR \fIchar\fR] [\fB-T\fR \fIdirectory\fR] [\fB-y\fR [\fIkmem\fR]]
20      [\fB-z\fR \fIrecsz\fR] [+\fIpos1\fR [-\fIpos2\fR]] [\fIfile\fR]...
21 .fi
23 .LP
24 .nf
25 \fB/usr/xpg4/bin/sort\fR [\fB-bcdfimMnru\fR] [\fB-k\fR \fIkeydef\fR] [\fB-o\fR \fIoutput\fR]
26      [\fB-S\fR \fIkmem\fR] [\fB-t\fR \fIchar\fR] [\fB-T\fR \fIdirectory\fR] [\fB-y\fR [\fIkmem\fR]]
27      [\fB-z\fR \fIrecsz\fR] [+\fIpos1\fR [-\fIpos2\fR]] [\fIfile\fR]...
28 .fi
30 .SH DESCRIPTION
31 .sp
32 .LP
33 The \fBsort\fR command sorts lines of all the named files together and writes
34 the result on the standard output.
35 .sp
36 .LP
37 Comparisons are based on one or more sort keys extracted from each line of
38 input. By default, there is one sort key, the entire input line. Lines are
39 ordered according to the collating sequence of the current locale.
40 .SH OPTIONS
41 .sp
42 .LP
43 The following options alter the default behavior:
44 .SS "/usr/bin/sort"
45 .sp
46 .ne 2
47 .na
48 \fB\fB-c\fR\fR
49 .ad
50 .RS 6n
51 Checks that the single input file is ordered as specified by the arguments and
52 the collating sequence of the current locale. The exit code is set and no
53 output is produced unless the file is out of sort.
54 .RE
56 .SS "/usr/xpg4/bin/sort"
57 .sp
58 .ne 2
59 .na
60 \fB\fB-c\fR\fR
61 .ad
62 .RS 16n
63 Same as \fB/usr/bin/sort\fR except no output is produced under any
64 circumstances.
65 .RE
67 .sp
68 .ne 2
69 .na
70 \fB\fB-m\fR\fR
71 .ad
72 .RS 16n
73 Merges only. The input files are assumed to be already sorted.
74 .RE
76 .sp
77 .ne 2
78 .na
79 \fB\fB-o\fR \fIoutput\fR\fR
80 .ad
81 .RS 16n
82 Specifies the name of an output file to be used instead of the standard output.
83 This file can be the same as one of the input files.
84 .RE
86 .sp
87 .ne 2
88 .na
89 \fB\fB-S\fR \fIkmem\fR\fR
90 .ad
91 .RS 16n
92 Specifies the maximum amount of swap-based memory used for sorting, in
93 kilobytes (the default unit). \fIkmem\fR can also be specified directly as a
94 number of bytes (b), kilobytes (k), megabytes (m), gigabytes (g), or terabytes
95 (t); or as a percentage (%) of the installed physical memory.
96 .RE
98 .sp
99 .ne 2
101 \fB\fB-T\fR \fIdirectory\fR\fR
103 .RS 16n
104 Specifies the \fIdirectory\fR in which to place temporary files.
108 .ne 2
110 \fB\fB-u\fR\fR
112 .RS 16n
113 Unique: suppresses all but one in each set of lines having equal keys. If used
114 with the \fB-c\fR option, checks that there are no lines with duplicate keys in
115 addition to checking that the input file is sorted.
119 .ne 2
121 \fB\fB-y\fR \fIkmem\fR\fR
123 .RS 16n
124 (obsolete). This option was used to specify the amount of main memory initially
125 used by \fBsort\fR. Its functionality is not appropriate for a virtual memory
126 system; memory usage for \fBsort\fR is now specified using the \fB-S\fR option.
130 .ne 2
132 \fB\fB-z\fR \fIrecsz\fR\fR
134 .RS 16n
135 (obsolete). This option was used to prevent abnormal termination when lines
136 longer than the system-dependent default buffer size are encountered. Because
137 \fBsort\fR automatically allocates buffers large enough to hold the longest
138 line, this option has no effect.
141 .SS "Ordering Options"
144 The default sort order depends on the value of \fBLC_COLLATE\fR. If
145 \fBLC_COLLATE\fR is set to \fBC\fR, sorting is in \fBASCII\fR order. If
146 \fBLC_COLLATE\fR is set to \fBen_US\fR, sorting is case insensitive except when
147 the two strings are otherwise equal and one has an uppercase letter earlier
148 than the other. Other locales have other sort orders.
151 The following options override the default ordering rules. When ordering
152 options appear independent of any key field specifications, the requested field
153 ordering rules are applied globally to all sort keys. When attached to a
154 specific key (see \fBSort Key Options\fR), the specified ordering options
155 override all global ordering options for that key. In the obsolescent forms, if
156 one or more of these options follows a \fI+pos1\fR option, it affects only the
157 key field specified by that preceding option.
159 .ne 2
161 \fB\fB-d\fR\fR
163 .RS 6n
164 Dictionary order: only letters, digits, and blanks (spaces and tabs) are
165 significant in comparisons.
169 .ne 2
171 \fB\fB-f\fR\fR
173 .RS 6n
174 Folds lower-case letters into upper case.
178 .ne 2
180 \fB\fB-i\fR\fR
182 .RS 6n
183 Ignores non-printable characters.
187 .ne 2
189 \fB\fB-M\fR\fR
191 .RS 6n
192 Compares as months. The first three non-blank characters of the field are
193 folded to upper case and compared. For example, in English the sorting order is
194 \fB"JAN" < "FEB" < .\|.\|. < "DEC"\fR. Invalid fields compare low to
195 \fB"JAN"\fR. The \fB-M\fR option implies the \fB-b\fR option (see below).
199 .ne 2
201 \fB\fB-n\fR\fR
203 .RS 6n
204 Restricts the sort key to an initial numeric string, consisting of optional
205 blank characters, optional minus sign, and zero or more digits with an optional
206 radix character and thousands separators (as defined in the current locale),
207 which is sorted by arithmetic value.  An empty digit string is treated as zero.
208 Leading zeros and signs on zeros do not affect ordering.
212 .ne 2
214 \fB\fB-r\fR\fR
216 .RS 6n
217 Reverses the sense of comparisons.
220 .SS "Field Separator Options"
223 The treatment of field separators can be altered using the following options:
225 .ne 2
227 \fB\fB-b\fR\fR
229 .RS 11n
230 Ignores leading blank characters when determining the starting and ending
231 positions of a restricted sort key. If the \fB-b\fR option is specified before
232 the first sort key option, it is applied to all sort key options. Otherwise,
233 the \fB-b\fR option can be attached independently to each \fB-k\fR
234 \fIfield_start\fR, \fIfield_end\fR, or +\fIpos1\fR or \(mi\fIpos2\fR
235 option-argument (see below).
239 .ne 2
241 \fB\fB-t\fR \fIchar\fR\fR
243 .RS 11n
244 Use \fIchar\fR as the field separator character. \fIchar\fR is not considered
245 to be part of a field (although it can be included in a sort key).  Each
246 occurrence of \fIchar\fR is significant (for example, \fI<char><char>\fR
247 delimits an empty field). If \fB-t\fR is not specified, blank characters are
248 used as default field separators; each maximal non-empty sequence of blank
249 characters that follows a non-blank character is a field separator.
252 .SS "Sort Key Options"
255 Sort keys can be specified using the options:
257 .ne 2
259 \fB\fB-k\fR \fIkeydef\fR\fR
261 .RS 19n
262 The \fIkeydef\fR argument is a restricted sort key field definition. The format
263 of this definition is:
265 .in +2
267 \fB-k\fR \fIfield_start\fR [\fItype\fR] [\fB,\fR\fIfield_end\fR [\fItype\fR] ]
269 .in -2
272 where:
274 .ne 2
276 \fB\fIfield_start\fR and \fIfield_end\fR\fR
278 .sp .6
279 .RS 4n
280 define a key field restricted to a portion of the line.
284 .ne 2
286 \fB\fItype\fR\fR
288 .sp .6
289 .RS 4n
290 is a modifier from the list of characters \fBbdfiMnr\fR. The \fBb\fR modifier
291 behaves like the \fB-b\fR option, but applies only to the \fIfield_start\fR or
292 \fIfield_end\fR to which it is attached and characters within a field are
293 counted from the first non-blank character in the field. (This applies
294 separately to \fIfirst_character\fR and \fIlast_character\fR.) The other
295 modifiers behave like the corresponding options, but apply only to the key
296 field to which they are attached. They have this effect if specified with
297 \fIfield_start\fR, \fIfield_end\fR or both.  If any modifier is attached to a
298 \fIfield_start\fR or to a \fIfield_end\fR, no option applies to either.
301 When there are multiple key fields, later keys are compared only after all
302 earlier keys compare equal. Except when the \fB-u\fR option is specified, lines
303 that otherwise compare equal are ordered as if none of the options \fB-d\fR,
304 \fB-f\fR, \fB-i\fR, \fB-n\fR or \fB-k\fR were present (but with \fB-r\fR still
305 in effect, if it was specified) and with all bytes in the lines significant to
306 the comparison.
308 The notation:
310 .in +2
312 \fB-k\fR \fIfield_start\fR[\fItype\fR][\fB,\fR\fIfield_end\fR[\fItype\fR]]
314 .in -2
317 defines a key field that begins at \fIfield_start\fR and ends at
318 \fIfield_end\fR inclusive, unless \fIfield_start\fR falls beyond the end of the
319 line or after \fIfield_end\fR, in which case the key field is empty. A missing
320 \fIfield_end\fR means the last character of the line.
322 A field comprises a maximal sequence of non-separating characters and, in the
323 absence of option \fB-t\fR, any preceding field separator.
325 The \fIfield_start\fR portion of the \fIkeydef\fR option-argument has the form:
327 .in +2
329 \fIfield_number\fR[\fB\&.\fR\fIfirst_character\fR]
331 .in -2
334 Fields and characters within fields are numbered starting with 1.
335 \fIfield_number\fR and \fIfirst_character\fR, interpreted as positive decimal
336 integers, specify the first character to be used as part of a sort key. If
337 \fB\&.\fR\fIfirst_character\fR is omitted, it refers to the first character of
338 the field.
340 The \fIfield_end\fR portion of the \fIkeydef\fR option-argument has the form:
342 .in +2
344 \fIfield_number\fR[\fB\&.\fR\fIlast_character\fR]
346 .in -2
349 The \fIfield_number\fR is as described above for \fIfield_start\fR.
350 \fIlast_character\fR, interpreted as a non-negative decimal integer, specifies
351 the last character to be used as part of the sort key. If \fIlast_character\fR
352 evaluates to zero or \fB\&.\fR\fIlast_character\fR is omitted, it refers to the
353 last character of the field specified by \fIfield_number\fR.
355 If the \fB-b\fR option or \fBb\fR type modifier is in effect, characters within
356 a field are counted from the first non-blank character in the field. (This
357 applies separately to \fIfirst_character\fR and \fIlast_character\fR.)
361 .ne 2
363 \fB[\fB+\fR\fIpos1\fR [\fB-\fR\fIpos2\fR]]\fR
365 .RS 19n
366 (obsolete). Provide functionality equivalent to the \fB-k\fR\fIkeydef\fR
367 option.
369 \fIpos1\fR and \fIpos2\fR each have the form \fIm\fR\fB\&.\fR\fIn\fR optionally
370 followed by one or more of the flags \fBbdfiMnr\fR. A starting position
371 specified by \fB+\fR\fIm\fR\fB\&.\fR\fIn\fR is interpreted to mean the
372 \fIn\fR+1st character in the \fIm\fR+1st field. A missing \fB\&.\fR\fIn\fR
373 means \fB\&.0\fR, indicating the first character of the \fIm\fR+1st field. If
374 the \fBb\fR flag is in effect \fIn\fR is counted from the first non-blank in
375 the \fIm\fR+1st field; \fB+\fR\fIm\fR\fB\&.0b\fR refers to the first non-blank
376 character in the \fIm\fR+1st field.
378 A last position specified by \fB\(mi\fR\fIm\fR\fB\&.\fR\fIn\fR is interpreted
379 to mean the \fIn\fRth character (including separators) after the last character
380 of the \fIm\fRth field. A missing \fB\&.\fR\fIn\fR means \fB\&.\fR0, indicating
381 the last character of the \fIm\fRth field. If the \fBb\fR flag is in effect
382 \fIn\fR is counted from the last leading blank in the \fIm\fR+1st field;
383 \fB\(mi\fR\fIm\fR\fB\&.\fR1\fBb\fR refers to the first non-blank in the
384 \fIm\fR+1st field.
386 The fully specified \fI+pos1\fR \fI\(mipos2\fR form with type modifiers \fBT\fR
387 and \fBU\fR:
389 .in +2
391 +\fBw\fR.\fBxT\fR -\fBy\fR.\fBzU\fR
393 .in -2
396 is equivalent to:
398 .in +2
400 undefined (z==0 & U contains \fIb\fR & \fI-t\fR is present)
401 -k w+1.x+1T,y.0U     (z==0 otherwise)
402 -k w+1.x+1T,y+1.zU   (z > 0)
404 .in -2
407 Implementations support at least nine occurrences of the sort keys (the
408 \fB-k\fR option and obsolescent \fB+\fR\fIpos1\fR and
409 \fB\(mi\fR\fIpos2\fR\fB)\fR which are significant in command line order. If no
410 sort key is specified, a default sort key of the entire line is used.
413 .SH OPERANDS
416 The following operand is supported:
418 .ne 2
420 \fB\fIfile\fR\fR
422 .RS 8n
423 A path name of a file to be sorted, merged or checked. If no \fIfile\fR
424 operands are specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard
425 input is used.
428 .SH USAGE
431 See \fBlargefile\fR(5) for the description of the behavior of \fBsort\fR when
432 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
433 .SH EXAMPLES
436 In the following examples, first the preferred and then the obsolete way of
437 specifying \fBsort\fR keys are given as an aid to understanding the
438 relationship between the two forms.
440 \fBExample 1 \fRSorting with the Second Field as a sort Key
443 Either of the following commands sorts the contents of \fBinfile\fR with the
444 second field as the sort key:
447 .in +2
449 example% \fBsort -k 2,2 infile\fR
450 example% \fBsort +1 \(mi2 infile\fR
452 .in -2
456 \fBExample 2 \fRSorting in Reverse Order
459 Either of the following commands sorts, in reverse order, the contents of
460 \fBinfile1\fR and \fBinfile2\fR, placing the output in \fBoutfile\fR and using
461 the second character of the second field as the sort key (assuming that the
462 first character of the second field is the field separator):
465 .in +2
467 example% \fBsort -r -o outfile -k 2.2,2.2 infile1 infile2\fR
468 example% \fBsort -r -o outfile +1.1 \(mi1.2 infile1 infile2\fR
470 .in -2
474 \fBExample 3 \fRSorting Using a Specified Character in One of the Files
477 Either of the following commands sorts the contents of \fBinfile1\fR and
478 \fBinfile2\fR using the second non-blank character of the second field as the
479 sort key:
482 .in +2
484 example% \fBsort -k 2.2b,2.2b infile1 infile2\fR
485 example% \fBsort +1.1b \(mi1.2b infile1 infile2\fR
487 .in -2
491 \fBExample 4 \fRSorting by Numeric User ID
494 Either of the following commands prints the \fBpasswd\fR(4) file (user
495 database) sorted by the numeric user ID (the third colon-separated field):
498 .in +2
500 example% \fBsort -t : -k 3,3n /etc/passwd\fR
501 example% \fBsort -t : +2 \(mi3n /etc/passwd\fR
503 .in -2
507 \fBExample 5 \fRPrinting Sorted Lines Excluding Lines that Duplicate a Field
510 Either of the following commands prints the lines of the already sorted file
511 \fBinfile\fR, suppressing all but one occurrence of lines having the same third
512 field:
515 .in +2
517 example% \fBsort -um -k 3.1,3.0 infile\fR
518 example% \fBsort -um +2.0 \(mi3.0 infile\fR
520 .in -2
524 \fBExample 6 \fRSorting by Host IP Address
527 Either of the following commands prints the \fBhosts\fR(4) file (IPv4 hosts
528 database), sorted by the numeric \fBIP\fR address (the first four numeric
529 fields):
532 .in +2
534 example$ \fBsort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts\fR
535 example$ \fBsort -t . +0 -1n +1 -2n +2 -3n +3 -4n /etc/hosts\fR
537 .in -2
542 Since '\fB\&.\fR' is both the field delimiter and, in many locales, the decimal
543 separator, failure to specify both ends of the field leads to results where the
544 second field is interpreted as a fractional portion of the first, and so forth.
546 .SH ENVIRONMENT VARIABLES
549 See \fBenviron\fR(5) for descriptions of the following environment variables
550 that affect the execution of \fBsort\fR: \fBLANG\fR, \fBLC_ALL\fR,
551 \fBLC_COLLATE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
553 .ne 2
555 \fB\fBLC_CTYPE\fR\fR
557 .RS 14n
558 Determine the locale for the interpretation of sequences of bytes of text data
559 as characters (for example, single- versus multi-byte characters in arguments
560 and input files) and the behavior of character classification for the \fB-b\fR,
561 \fB-d\fR, \fB-f\fR, \fB-i\fR and \fB-n\fR options.
565 .ne 2
567 \fB\fBLC_NUMERIC\fR\fR
569 .RS 14n
570 Determine the locale for the definition of the radix character and thousands
571 separator for the \fB-n\fR option.
574 .SH EXIT STATUS
577 The following exit values are returned:
579 .ne 2
581 \fB\fB0\fR\fR
583 .RS 6n
584 All input files were output successfully, or \fB-c\fR was specified and the
585 input file was correctly sorted.
589 .ne 2
591 \fB\fB1\fR\fR
593 .RS 6n
594 Under the \fB-c\fR option, the file was not ordered as specified, or if the
595 \fB-c\fR and \fB-u\fR options were both specified, two input lines were found
596 with equal keys.
600 .ne 2
602 \fB\fB>1\fR\fR
604 .RS 6n
605 An error occurred.
608 .SH FILES
610 .ne 2
612 \fB\fB/var/tmp/stm???\fR\fR
614 .RS 19n
615 Temporary files
618 .SH ATTRIBUTES
621 See \fBattributes\fR(5) for descriptions of the following attributes:
622 .SS "/usr/bin/sort"
627 box;
628 c | c
629 l | l .
630 ATTRIBUTE TYPE  ATTRIBUTE VALUE
632 CSI     Enabled
635 .SS "/usr/xpg4/bin/sort"
640 box;
641 c | c
642 l | l .
643 ATTRIBUTE TYPE  ATTRIBUTE VALUE
645 CSI     Enabled
647 Interface Stability     Standard
650 .SH SEE ALSO
653 \fBcomm\fR(1), \fBjoin\fR(1), \fBuniq\fR(1), \fBnl_langinfo\fR(3C),
654 \fBstrftime\fR(3C), \fBhosts\fR(4), \fBpasswd\fR(4), \fBattributes\fR(5),
655 \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
656 .SH DIAGNOSTICS
659 Comments and exits with non-zero status for various trouble conditions (for
660 example, when input lines are too long), and for disorders discovered under the
661 \fB-c\fR option.
662 .SH NOTES
665 When the last line of an input file is missing a \fBnew-line\fR character,
666 \fBsort\fR appends one, prints a warning message, and continues.
669 \fBsort\fR does not guarantee preservation of relative line ordering on equal
670 keys.
673 One can tune \fBsort\fR performance for a specific scenario using the \fB-S\fR
674 option. However, one should note in particular that \fBsort\fR has greater
675 knowledge of how to use a finite amount of memory for sorting than the virtual
676 memory system. Thus, a sort invoked to request an extremely large amount of
677 memory via the \fB-S\fR option could perform extremely poorly.
680 As noted, certain of the field modifiers (such as \fB-M\fR and \fB-d\fR) cause
681 the interpretation of input data to be done with reference to locale-specific
682 settings. The results of this interpretation can be unexpected if one's
683 expectations are not aligned with the conventions established by the locale. In
684 the case of the month keys, \fBsort\fR does not attempt to compensate for
685 approximate month abbreviations. The precise month abbreviations from
686 \fBnl_langinfo\fR(3C) or \fBstrftime\fR(3C) are the only ones recognized. For
687 printable or dictionary order, if these concepts are not well-defined by the
688 locale, an empty sort key might be the result, leading to the next key being
689 the significant one for determining the appropriate ordering.