Commit | Line | Data |
---|---|---|
379847a8 WJ |
1 | .TH SORT 1L \" -*- nroff -*- |
2 | .SH NAME | |
3 | sort \- sort lines of text files | |
4 | .SH SYNOPSIS | |
5 | .B sort | |
6 | [\-cmus] [\-t separator] [\-o output-file] [\-bdfiMnr] [+POS1 [\-POS2]] | |
7 | [\-k POS1[,POS2]] [file...] | |
8 | .SH DESCRIPTION | |
9 | This manual page | |
10 | documents the GNU version of | |
11 | .BR sort . | |
12 | .B sort | |
13 | sorts, merges, or compares all the lines from the given files, or the standard | |
14 | input if no files are given. A file name of `-' means standard input. | |
15 | By default, | |
16 | .B sort | |
17 | writes the results to the standard output. | |
18 | .PP | |
19 | .B sort | |
20 | has three modes of operation: sort (the default), merge, and check for | |
21 | sortedness. The following options change the operation mode: | |
22 | .TP | |
23 | .I \-c | |
24 | Check whether the given files are already sorted: if they are not all | |
25 | sorted, print an error message and exit with a status of 1. | |
26 | .TP | |
27 | .I \-m | |
28 | Merge the given files by sorting them as a group. Each input file | |
29 | should already be individually sorted. It always works to sort | |
30 | instead of merge; merging is provided because it is faster, in the | |
31 | case where it works. | |
32 | .PP | |
33 | A pair of lines is compared as follows: | |
34 | if any key fields have been specified, | |
35 | .B sort | |
36 | compares each pair of fields, in the order specified on the command | |
37 | line, according to the associated ordering options, until a difference | |
38 | is found or no fields are left. | |
39 | .PP | |
40 | If any of the global options | |
41 | .I Mbdfinr | |
42 | are given but no key fields are | |
43 | specified, | |
44 | .B sort | |
45 | compares the entire lines according to the global options. | |
46 | .PP | |
47 | Finally, as a last resort when all keys compare equal | |
48 | (or if no ordering options were specified at all), | |
49 | .B sort | |
50 | compares the lines byte by byte in machine collating sequence. The | |
51 | .I \-s | |
52 | option disables this last resort comparison, producing a stable sort. | |
53 | .PP | |
54 | GNU | |
55 | .B sort | |
56 | has no limits on input line length or restrictions on bytes allowed | |
57 | within lines. In addition, if the final byte of an input file is not | |
58 | a newline, GNU | |
59 | .B sort | |
60 | silently supplies one. In some cases, such as exactly what the | |
61 | .I \-b | |
62 | and | |
63 | .I \-f | |
64 | options do, BSD and System V | |
65 | .B sort | |
66 | programs produce different output; GNU | |
67 | .B sort | |
68 | follows the POSIX behavior, which is usually like the System V behavior. | |
69 | .PP | |
70 | If the environment variable TMPDIR is set, | |
71 | .B sort | |
72 | uses it as the directory in which to put temporary files instead of | |
73 | the default, /tmp. | |
74 | .PP | |
75 | The following options affect the ordering of output lines. They may | |
76 | be specified globally or as part of a specific key field. If no key | |
77 | fields are specified, global options apply to comparison of entire | |
78 | lines; otherwise the global options are inherited by key fields that | |
79 | do not specify any special options of their own. | |
80 | .TP | |
81 | .I \-b | |
82 | Ignore leading blanks when finding sort keys in each line. | |
83 | .TP | |
84 | .I \-d | |
85 | Sort in `dictionary order': ignore all characters except letters, | |
86 | digits and blanks when sorting. | |
87 | .TP | |
88 | .I \-f | |
89 | Fold lower case characters into the equivalent upper case characters | |
90 | when sorting so that, for example, `b' is sorted the same way `B' is. | |
91 | .TP | |
92 | .I \-i | |
93 | Ignore characters outside the ASCII range 040-0176 (inclusive) when sorting. | |
94 | .TP | |
95 | .I \-M | |
96 | An initial string, consisting of any amount of white space, followed | |
97 | by three letters abbreviating a month name, is folded to lower case | |
98 | and compared in the order `jan' < `feb' < ... < `dec.' Invalid names | |
99 | compare low to valid names. This option implies | |
100 | .IR \-b . | |
101 | .TP | |
102 | .I \-n | |
103 | Compare according to arithmetic value an initial numeric string | |
104 | consisting of optional white space, an optional \- sign, and zero or | |
105 | more digits, optionally followed by a decimal point and zero or more | |
106 | digits. This option implies | |
107 | .IR \-b . | |
108 | .TP | |
109 | .I \-r | |
110 | Reverse the result of comparison, so that lines with greater key | |
111 | values appear earlier in the output instead of later. | |
112 | .PP | |
113 | Other options are: | |
114 | .TP | |
115 | .I "\-o output-file" | |
116 | Write output to | |
117 | .I output-file | |
118 | instead of to the standard output. If | |
119 | .I output-file | |
120 | is one of the input files, | |
121 | .B sort | |
122 | copies it to a temporary file before sorting and writing the output to | |
123 | .IR output-file . | |
124 | .TP | |
125 | .I "\-t separator" | |
126 | Use character | |
127 | .I separator | |
128 | as the field separator when finding the sort keys in each line. By | |
129 | default, fields are separated by the empty string between a | |
130 | non-whitespace character and a whitespace character. That is to say, | |
131 | given the input line ` foo bar', | |
132 | .B sort | |
133 | breaks it into fields ` foo' and ` bar'. The field separator is not | |
134 | considered to be part of either the field preceding or the field | |
135 | following it. | |
136 | .TP | |
137 | .I \-u | |
138 | For the default case or the | |
139 | .I \-m | |
140 | option, only output the first of a sequence of lines that compare | |
141 | equal. For the | |
142 | .I \-c | |
143 | option, check that no pair of consecutive lines compares equal. | |
144 | .TP | |
145 | .I "+POS1 [\-POS2]" | |
146 | Specify a field within each line to use as a sorting key. The field | |
147 | consists of the portion of the line starting at POS1 and up to (but | |
148 | not including) POS2 (or to the end of the line if POS2 is not given). | |
149 | The fields and character positions are numbered starting with 0. | |
150 | .TP | |
151 | .I "\-k POS1[,POS2]" | |
152 | An alternate syntax for specifying sorting keys. | |
153 | The fields and character positions are numbered starting with 1. | |
154 | .PP | |
155 | A position has the form \fIf\fP.\fIc\fP, where \fIf\fP is the number | |
156 | of the field to use and \fIc\fP is the number of the first character | |
157 | from the beginning of the field (for \fI+pos\fP) or from the end of | |
158 | the previous field (for \fI\-pos\fP). The .\fIc\fP part of a position | |
159 | may be omitted in which case it is taken to be the first character in | |
160 | the field. If the | |
161 | .I \-b | |
162 | option has been given, the .\fIc\fP part of a field specification is | |
163 | counted from the first nonblank character of the field (for | |
164 | \fI+pos\fP) or from the first nonblank character following the | |
165 | previous field (for \fI\-pos\fP). | |
166 | .PP | |
167 | A \fI+pos\fP or \fI-pos\fP argument may also have any of the option | |
168 | letters | |
169 | .I Mbdfinr | |
170 | appended to it, in which case the global ordering options are not used | |
171 | for that particular field. The | |
172 | .I \-b | |
173 | option may be independently attached to either or both of the | |
174 | \fI+pos\fP and \fI\-pos\fP parts of a field specification, and if it | |
175 | is inherited from the global options it will be attached to both. | |
176 | If a | |
177 | .I \-n | |
178 | or | |
179 | .I \-M | |
180 | option is used, thus implying a | |
181 | .I \-b | |
182 | option, the | |
183 | .I \-b | |
184 | option is taken to apply to both the \fI+pos\fP and the \fI\-pos\fP | |
185 | parts of a key specification. Keys may span multiple fields. |