2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
25 .\" This notice shall appear on any product containing this material.
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
45 .\" Copyright (c) 2001, Sun Microsystems, Inc. All Rights Reserved
47 .TH SORT 1 "Nov 19, 2001"
49 sort \- sort, merge, or sequence check text files
53 \fB/usr/bin/sort\fR [\fB-bcdfimMnru\fR] [\fB-k\fR \fIkeydef\fR] [\fB-o\fR \fIoutput\fR]
54 [\fB-S\fR \fIkmem\fR] [\fB-t\fR \fIchar\fR] [\fB-T\fR \fIdirectory\fR] [\fB-y\fR [\fIkmem\fR]]
55 [\fB-z\fR \fIrecsz\fR] [+\fIpos1\fR [-\fIpos2\fR]] [\fIfile\fR]...
60 \fB/usr/xpg4/bin/sort\fR [\fB-bcdfimMnru\fR] [\fB-k\fR \fIkeydef\fR] [\fB-o\fR \fIoutput\fR]
61 [\fB-S\fR \fIkmem\fR] [\fB-t\fR \fIchar\fR] [\fB-T\fR \fIdirectory\fR] [\fB-y\fR [\fIkmem\fR]]
62 [\fB-z\fR \fIrecsz\fR] [+\fIpos1\fR [-\fIpos2\fR]] [\fIfile\fR]...
68 The \fBsort\fR command sorts lines of all the named files together and writes
69 the result on the standard output.
72 Comparisons are based on one or more sort keys extracted from each line of
73 input. By default, there is one sort key, the entire input line. Lines are
74 ordered according to the collating sequence of the current locale.
78 The following options alter the default behavior:
86 Checks that the single input file is ordered as specified by the arguments and
87 the collating sequence of the current locale. The exit code is set and no
88 output is produced unless the file is out of sort.
91 .SS "/usr/xpg4/bin/sort"
98 Same as \fB/usr/bin/sort\fR except no output is produced under any
108 Merges only. The input files are assumed to be already sorted.
114 \fB\fB-o\fR \fIoutput\fR\fR
117 Specifies the name of an output file to be used instead of the standard output.
118 This file can be the same as one of the input files.
124 \fB\fB-S\fR \fIkmem\fR\fR
127 Specifies the maximum amount of swap-based memory used for sorting, in
128 kilobytes (the default unit). \fIkmem\fR can also be specified directly as a
129 number of bytes (b), kilobytes (k), megabytes (m), gigabytes (g), or terabytes
130 (t); or as a percentage (%) of the installed physical memory.
136 \fB\fB-T\fR \fIdirectory\fR\fR
139 Specifies the \fIdirectory\fR in which to place temporary files.
148 Unique: suppresses all but one in each set of lines having equal keys. If used
149 with the \fB-c\fR option, checks that there are no lines with duplicate keys in
150 addition to checking that the input file is sorted.
156 \fB\fB-y\fR \fIkmem\fR\fR
159 (obsolete). This option was used to specify the amount of main memory initially
160 used by \fBsort\fR. Its functionality is not appropriate for a virtual memory
161 system; memory usage for \fBsort\fR is now specified using the \fB-S\fR option.
167 \fB\fB-z\fR \fIrecsz\fR\fR
170 (obsolete). This option was used to prevent abnormal termination when lines
171 longer than the system-dependent default buffer size are encountered. Because
172 \fBsort\fR automatically allocates buffers large enough to hold the longest
173 line, this option has no effect.
176 .SS "Ordering Options"
179 The default sort order depends on the value of \fBLC_COLLATE\fR. If
180 \fBLC_COLLATE\fR is set to \fBC\fR, sorting is in \fBASCII\fR order. If
181 \fBLC_COLLATE\fR is set to \fBen_US\fR, sorting is case insensitive except when
182 the two strings are otherwise equal and one has an uppercase letter earlier
183 than the other. Other locales have other sort orders.
186 The following options override the default ordering rules. When ordering
187 options appear independent of any key field specifications, the requested field
188 ordering rules are applied globally to all sort keys. When attached to a
189 specific key (see \fBSort Key Options\fR), the specified ordering options
190 override all global ordering options for that key. In the obsolescent forms, if
191 one or more of these options follows a \fI+pos1\fR option, it affects only the
192 key field specified by that preceding option.
199 Dictionary order: only letters, digits, and blanks (spaces and tabs) are
200 significant in comparisons.
209 Folds lower-case letters into upper case.
218 Ignores non-printable characters.
227 Compares as months. The first three non-blank characters of the field are
228 folded to upper case and compared. For example, in English the sorting order is
229 \fB"JAN" < "FEB" < .\|.\|. < "DEC"\fR. Invalid fields compare low to
230 \fB"JAN"\fR. The \fB-M\fR option implies the \fB-b\fR option (see below).
239 Restricts the sort key to an initial numeric string, consisting of optional
240 blank characters, optional minus sign, and zero or more digits with an optional
241 radix character and thousands separators (as defined in the current locale),
242 which is sorted by arithmetic value. An empty digit string is treated as zero.
243 Leading zeros and signs on zeros do not affect ordering.
252 Reverses the sense of comparisons.
255 .SS "Field Separator Options"
258 The treatment of field separators can be altered using the following options:
265 Ignores leading blank characters when determining the starting and ending
266 positions of a restricted sort key. If the \fB-b\fR option is specified before
267 the first sort key option, it is applied to all sort key options. Otherwise,
268 the \fB-b\fR option can be attached independently to each \fB-k\fR
269 \fIfield_start\fR, \fIfield_end\fR, or +\fIpos1\fR or \(mi\fIpos2\fR
270 option-argument (see below).
276 \fB\fB-t\fR \fIchar\fR\fR
279 Use \fIchar\fR as the field separator character. \fIchar\fR is not considered
280 to be part of a field (although it can be included in a sort key). Each
281 occurrence of \fIchar\fR is significant (for example, \fI<char><char>\fR
282 delimits an empty field). If \fB-t\fR is not specified, blank characters are
283 used as default field separators; each maximal non-empty sequence of blank
284 characters that follows a non-blank character is a field separator.
287 .SS "Sort Key Options"
290 Sort keys can be specified using the options:
294 \fB\fB-k\fR \fIkeydef\fR\fR
297 The \fIkeydef\fR argument is a restricted sort key field definition. The format
298 of this definition is:
302 \fB-k\fR \fIfield_start\fR [\fItype\fR] [\fB,\fR\fIfield_end\fR [\fItype\fR] ]
311 \fB\fIfield_start\fR and \fIfield_end\fR\fR
315 define a key field restricted to a portion of the line.
325 is a modifier from the list of characters \fBbdfiMnr\fR. The \fBb\fR modifier
326 behaves like the \fB-b\fR option, but applies only to the \fIfield_start\fR or
327 \fIfield_end\fR to which it is attached and characters within a field are
328 counted from the first non-blank character in the field. (This applies
329 separately to \fIfirst_character\fR and \fIlast_character\fR.) The other
330 modifiers behave like the corresponding options, but apply only to the key
331 field to which they are attached. They have this effect if specified with
332 \fIfield_start\fR, \fIfield_end\fR or both. If any modifier is attached to a
333 \fIfield_start\fR or to a \fIfield_end\fR, no option applies to either.
336 When there are multiple key fields, later keys are compared only after all
337 earlier keys compare equal. Except when the \fB-u\fR option is specified, lines
338 that otherwise compare equal are ordered as if none of the options \fB-d\fR,
339 \fB-f\fR, \fB-i\fR, \fB-n\fR or \fB-k\fR were present (but with \fB-r\fR still
340 in effect, if it was specified) and with all bytes in the lines significant to
347 \fB-k\fR \fIfield_start\fR[\fItype\fR][\fB,\fR\fIfield_end\fR[\fItype\fR]]
352 defines a key field that begins at \fIfield_start\fR and ends at
353 \fIfield_end\fR inclusive, unless \fIfield_start\fR falls beyond the end of the
354 line or after \fIfield_end\fR, in which case the key field is empty. A missing
355 \fIfield_end\fR means the last character of the line.
357 A field comprises a maximal sequence of non-separating characters and, in the
358 absence of option \fB-t\fR, any preceding field separator.
360 The \fIfield_start\fR portion of the \fIkeydef\fR option-argument has the form:
364 \fIfield_number\fR[\fB\&.\fR\fIfirst_character\fR]
369 Fields and characters within fields are numbered starting with 1.
370 \fIfield_number\fR and \fIfirst_character\fR, interpreted as positive decimal
371 integers, specify the first character to be used as part of a sort key. If
372 \fB\&.\fR\fIfirst_character\fR is omitted, it refers to the first character of
375 The \fIfield_end\fR portion of the \fIkeydef\fR option-argument has the form:
379 \fIfield_number\fR[\fB\&.\fR\fIlast_character\fR]
384 The \fIfield_number\fR is as described above for \fIfield_start\fR.
385 \fIlast_character\fR, interpreted as a non-negative decimal integer, specifies
386 the last character to be used as part of the sort key. If \fIlast_character\fR
387 evaluates to zero or \fB\&.\fR\fIlast_character\fR is omitted, it refers to the
388 last character of the field specified by \fIfield_number\fR.
390 If the \fB-b\fR option or \fBb\fR type modifier is in effect, characters within
391 a field are counted from the first non-blank character in the field. (This
392 applies separately to \fIfirst_character\fR and \fIlast_character\fR.)
398 \fB[\fB+\fR\fIpos1\fR [\fB-\fR\fIpos2\fR]]\fR
401 (obsolete). Provide functionality equivalent to the \fB-k\fR\fIkeydef\fR
404 \fIpos1\fR and \fIpos2\fR each have the form \fIm\fR\fB\&.\fR\fIn\fR optionally
405 followed by one or more of the flags \fBbdfiMnr\fR. A starting position
406 specified by \fB+\fR\fIm\fR\fB\&.\fR\fIn\fR is interpreted to mean the
407 \fIn\fR+1st character in the \fIm\fR+1st field. A missing \fB\&.\fR\fIn\fR
408 means \fB\&.0\fR, indicating the first character of the \fIm\fR+1st field. If
409 the \fBb\fR flag is in effect \fIn\fR is counted from the first non-blank in
410 the \fIm\fR+1st field; \fB+\fR\fIm\fR\fB\&.0b\fR refers to the first non-blank
411 character in the \fIm\fR+1st field.
413 A last position specified by \fB\(mi\fR\fIm\fR\fB\&.\fR\fIn\fR is interpreted
414 to mean the \fIn\fRth character (including separators) after the last character
415 of the \fIm\fRth field. A missing \fB\&.\fR\fIn\fR means \fB\&.\fR0, indicating
416 the last character of the \fIm\fRth field. If the \fBb\fR flag is in effect
417 \fIn\fR is counted from the last leading blank in the \fIm\fR+1st field;
418 \fB\(mi\fR\fIm\fR\fB\&.\fR1\fBb\fR refers to the first non-blank in the
421 The fully specified \fI+pos1\fR \fI\(mipos2\fR form with type modifiers \fBT\fR
426 +\fBw\fR.\fBxT\fR -\fBy\fR.\fBzU\fR
435 undefined (z==0 & U contains \fIb\fR & \fI-t\fR is present)
436 -k w+1.x+1T,y.0U (z==0 otherwise)
437 -k w+1.x+1T,y+1.zU (z > 0)
442 Implementations support at least nine occurrences of the sort keys (the
443 \fB-k\fR option and obsolescent \fB+\fR\fIpos1\fR and
444 \fB\(mi\fR\fIpos2\fR\fB)\fR which are significant in command line order. If no
445 sort key is specified, a default sort key of the entire line is used.
451 The following operand is supported:
458 A path name of a file to be sorted, merged or checked. If no \fIfile\fR
459 operands are specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard
466 See \fBlargefile\fR(5) for the description of the behavior of \fBsort\fR when
467 encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
471 In the following examples, first the preferred and then the obsolete way of
472 specifying \fBsort\fR keys are given as an aid to understanding the
473 relationship between the two forms.
475 \fBExample 1 \fRSorting with the Second Field as a sort Key
478 Either of the following commands sorts the contents of \fBinfile\fR with the
479 second field as the sort key:
484 example% \fBsort -k 2,2 infile\fR
485 example% \fBsort +1 \(mi2 infile\fR
491 \fBExample 2 \fRSorting in Reverse Order
494 Either of the following commands sorts, in reverse order, the contents of
495 \fBinfile1\fR and \fBinfile2\fR, placing the output in \fBoutfile\fR and using
496 the second character of the second field as the sort key (assuming that the
497 first character of the second field is the field separator):
502 example% \fBsort -r -o outfile -k 2.2,2.2 infile1 infile2\fR
503 example% \fBsort -r -o outfile +1.1 \(mi1.2 infile1 infile2\fR
509 \fBExample 3 \fRSorting Using a Specified Character in One of the Files
512 Either of the following commands sorts the contents of \fBinfile1\fR and
513 \fBinfile2\fR using the second non-blank character of the second field as the
519 example% \fBsort -k 2.2b,2.2b infile1 infile2\fR
520 example% \fBsort +1.1b \(mi1.2b infile1 infile2\fR
526 \fBExample 4 \fRSorting by Numeric User ID
529 Either of the following commands prints the \fBpasswd\fR(4) file (user
530 database) sorted by the numeric user ID (the third colon-separated field):
535 example% \fBsort -t : -k 3,3n /etc/passwd\fR
536 example% \fBsort -t : +2 \(mi3n /etc/passwd\fR
542 \fBExample 5 \fRPrinting Sorted Lines Excluding Lines that Duplicate a Field
545 Either of the following commands prints the lines of the already sorted file
546 \fBinfile\fR, suppressing all but one occurrence of lines having the same third
552 example% \fBsort -um -k 3.1,3.0 infile\fR
553 example% \fBsort -um +2.0 \(mi3.0 infile\fR
559 \fBExample 6 \fRSorting by Host IP Address
562 Either of the following commands prints the \fBhosts\fR(4) file (IPv4 hosts
563 database), sorted by the numeric \fBIP\fR address (the first four numeric
569 example$ \fBsort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts\fR
570 example$ \fBsort -t . +0 -1n +1 -2n +2 -3n +3 -4n /etc/hosts\fR
577 Since '\fB\&.\fR' is both the field delimiter and, in many locales, the decimal
578 separator, failure to specify both ends of the field leads to results where the
579 second field is interpreted as a fractional portion of the first, and so forth.
581 .SH ENVIRONMENT VARIABLES
584 See \fBenviron\fR(5) for descriptions of the following environment variables
585 that affect the execution of \fBsort\fR: \fBLANG\fR, \fBLC_ALL\fR,
586 \fBLC_COLLATE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
593 Determine the locale for the interpretation of sequences of bytes of text data
594 as characters (for example, single- versus multi-byte characters in arguments
595 and input files) and the behavior of character classification for the \fB-b\fR,
596 \fB-d\fR, \fB-f\fR, \fB-i\fR and \fB-n\fR options.
602 \fB\fBLC_NUMERIC\fR\fR
605 Determine the locale for the definition of the radix character and thousands
606 separator for the \fB-n\fR option.
612 The following exit values are returned:
619 All input files were output successfully, or \fB-c\fR was specified and the
620 input file was correctly sorted.
629 Under the \fB-c\fR option, the file was not ordered as specified, or if the
630 \fB-c\fR and \fB-u\fR options were both specified, two input lines were found
647 \fB\fB/var/tmp/stm???\fR\fR
656 See \fBattributes\fR(5) for descriptions of the following attributes:
665 ATTRIBUTE TYPE ATTRIBUTE VALUE
670 .SS "/usr/xpg4/bin/sort"
678 ATTRIBUTE TYPE ATTRIBUTE VALUE
682 Interface Stability Standard
688 \fBcomm\fR(1), \fBjoin\fR(1), \fBuniq\fR(1), \fBnl_langinfo\fR(3C),
689 \fBstrftime\fR(3C), \fBhosts\fR(4), \fBpasswd\fR(4), \fBattributes\fR(5),
690 \fBenviron\fR(5), \fBlargefile\fR(5), \fBstandards\fR(5)
694 Comments and exits with non-zero status for various trouble conditions (for
695 example, when input lines are too long), and for disorders discovered under the
700 When the last line of an input file is missing a \fBnew-line\fR character,
701 \fBsort\fR appends one, prints a warning message, and continues.
704 \fBsort\fR does not guarantee preservation of relative line ordering on equal
708 One can tune \fBsort\fR performance for a specific scenario using the \fB-S\fR
709 option. However, one should note in particular that \fBsort\fR has greater
710 knowledge of how to use a finite amount of memory for sorting than the virtual
711 memory system. Thus, a sort invoked to request an extremely large amount of
712 memory via the \fB-S\fR option could perform extremely poorly.
715 As noted, certain of the field modifiers (such as \fB-M\fR and \fB-d\fR) cause
716 the interpretation of input data to be done with reference to locale-specific
717 settings. The results of this interpretation can be unexpected if one's
718 expectations are not aligned with the conventions established by the locale. In
719 the case of the month keys, \fBsort\fR does not attempt to compensate for
720 approximate month abbreviations. The precise month abbreviations from
721 \fBnl_langinfo\fR(3C) or \fBstrftime\fR(3C) are the only ones recognized. For
722 printable or dictionary order, if these concepts are not well-defined by the
723 locale, an empty sort key might be the result, leading to the next key being
724 the significant one for determining the appropriate ordering.