OSDN Git Service

5e3538f5b5bf6fb6e06e3c4d8ca01154b3fb1b85
[linuxjm/jm.git] / manual / gnumaniak / original / man1 / sort.1
1 .\" You may copy, distribute and modify under the terms of the LDP General
2 .\" Public License as specified in the LICENSE file that comes with the
3 .\" gnumaniak distribution
4 .\"
5 .\" The author kindly requests that no comments regarding the "better"
6 .\" suitability or up-to-date notices of any info documentation alternative
7 .\" is added without contacting him first.
8 .\"
9 .\" (C) 2002 Ragnar Hojland Espinosa <ragnar@ragnar-hojland.com>
10 .\"
11 .\"     GNU sort man page
12 .\"     man pages are NOT obsolete!
13 .\"     <ragnar@ragnar-hojland.com>
14 .TH SORT 1 "7 October 2002" "GNU textutils 2.1"
15 .SH NAME
16 \fBsort\fR \- sort text files
17 .SH SYNOPSIS
18 .B sort
19 .RB [ \-cm ]
20 .RB [ \-bdfginruzM ]
21 .RB [ "\+\fIPOS1\fR[\fB\-\fIPOS2" ]
22 .RB [ "\-o \fIOUTFILE" ]
23 .RB [ "\-t \fISEPARATOR" ]
24 .RB [ "\-k \fIPOS1\fR[\fB,\fIPOS2" ]
25 .RB [ "\-K \fIPOS1\fR[\fB,\fIPOS2" ]
26 .RB [ "\-S \fISIZE\fR" ]
27 .RB [ "\-T \fITEMPDIR" ]
28 .RB [ \-\-reverse ]
29 .RI [ FILE... ]
30
31 .BR sort " [" \-\-help "] [" \-\-version ]
32 .SH DESCRIPTION
33 .B sort
34 sorts, merges, or compares all the lines from any given
35 .IR FILE s.
36 concatenating the result and writing it to standard output
37 If
38 .I FILE
39 is omitted or if it is a
40 .RB ` \- ',
41 standard input is used for reading.
42
43 GNU \fBsort\fR (as specified for all GNU utilities) has no limits on
44 input line length or restrictions on bytes allowed within lines.  In
45 addition, if the final byte of an input file is not a newline,
46 \fBsort\fR silently supplies one.
47    
48 Upon any error, \fBsort\fR exits with a status of 2'.
49       
50 If the environment variable \fBTMPDIR\fR is set, \fBsort\fR uses its value
51 as the directory for temporary files instead of `/tmp'.  The `\fB\-T
52 \fiTEMPDIR\fR' option in turn overrides the environment variable.
53
54 A position in a sort field specified with the \fB\-k\fR or \fB+\fR option
55 has the form \fIF\fB.\fIC\fR, where \fIF\fR is the number of the field to
56 use and \fIC\fR is the number of the first character from the beginning of
57 the field (for \fB+\fIPOS\fR) or from the end of the previous field (for
58 \fB\-\fIPOS\fR).  If the \fB.\fIC\fR is omitted, it is taken to be the first
59 character in the field.  If the \fB\-b\fR option was specified, the
60 \fB.\fIC\fR part of a field specification is counted from the first nonblank
61 character of the field (for \fB+\fIPOS\fR) or from the first nonblank
62 character following the previous field (for \fB\-\fIPOS\fR).
63
64 A sort key option may also have any of the output ordering options
65 appended to it, in which case the global ordering options are not used
66 for that particular field.  The \fB\-b\fR option may be independently
67 attached to either or both of the \fB+\fIPOS\fR and \fB\-\fIPOS\fR parts of
68 a field specification, and if it is inherited from the global options it
69 will be attached to both.  Keys may span multiple fields.
70 .SH OPTIONS
71 .SS Behaviour
72 The default behaviour for \fBsort\fR is to sort.  You may change this
73 behaviour with:
74 .TP
75 .B \-c
76 Check whether the given files are already sorted: if they are not
77 all sorted, print an error message and exit with a status of 1.
78 Otherwise, exit successfully.         
79 .TP
80 .B \-m
81 Merge the given files by sorting them as a group.  Each input file
82 must always be individually sorted.  It always works to sort
83 instead of merge; merging is provided because it is faster, in the
84 case where it works.
85 .PP
86 A pair of lines is compared as follows: if any key fields have been
87 specified, \fBsort\fR compares each pair of fields, in the order specified
88 on the command line, according to the associated ordering options,
89 until a difference is found or no fields are left.
90
91 If any of the global options `\fB\-bdfinrM\fR' are given but no key fields
92 are specified, \fBsort\fR compares the entire lines according to the global
93 options.
94
95 Finally, as a last resort when all keys compare equal (or if no
96 ordering options were specified at all), \fNsort\fR compares the lines byte
97 by byte in machine collating sequence instead of \fBLC_COLLATE\fR.  The last
98 resort comparison honors the \fB\-r\fR global option.  The \fB\-s\fR
99 (stable) option disables this last-resort comparison so that lines in which
100 all fields compare equal are left in their original relative order.
101 \fB\-s\fR has no effect if no fields or global options are specified.
102 .SS Output Ordering Options
103 The following options affect how \fBsort\fR orders output lines.  They may
104 be specified globally or as part of a specific key field.  If no key
105 fields are specified, global options apply to comparison of entire
106 lines; otherwise the global options are inherited by key fields that do
107 not specify any special options of their own.
108 .TP
109 .B \-b
110 Ignore leading blanks when finding sort keys in each line.
111 .TP    
112 .B \-d
113 Sort in "phone directory" order: ignore all characters except
114 letters, digits and blanks when sorting.
115 .TP
116 .B \-f
117 Fold lowercase characters into the equivalent uppercase characters
118 when sorting so that, for example, `b' and `B' sort as equal.
119 .TP
120 .B \-g
121 Sort numerically using \fBstrtod\fR(3) to arrive at the numeric
122 values.  This allows floating point numbers to be specified in
123 scientific notation, like `1.0e-34' and `10e100'.  Use this option
124 only if there is no alternative;  it is much slower than \fB\-n\fR and
125 numbers with too many significant digits will be compared as if
126 they had been truncated.  In addition, numbers outside the range
127 of representable double precision floating point numbers are
128 treated as if they were zeroes; overflows, underflows and conversion
129 errors are not reported.
130 .TP
131 .B \-i
132 Ignore unprintable characters.
133 .TP
134 .B \-n
135 Sort numerically: the number begins each line; specifically, it
136 consists of optional whitespace, an optional `-' sign, and zero or
137 more digits, optionally followed by a decimal point and zero or
138 more digits.
139 .sp
140 \fBsort \-n\fR uses what might be considered an unconventional method
141 to compare strings representing floating point numbers.  Rather than first
142 converting each string to the C \fBdouble\fR type and then comparing those
143 values, sort aligns the radix points in the two strings and compares the
144 strings a character at a time.  One benefit of using this approach is its
145 speed.  In practice this is much more efficient than performing the two
146 corresponding string-to-double (or even string-to-integer) conversions and
147 then comparing doubles.  In addition, there is no corresponding loss of
148 precision.  Converting each string to \fBdouble\fR before comparison
149 would limit precision to about 16 digits on most systems.
150           
151 Neither a leading `+' nor exponential notation is recognized.  To
152 compare such strings numerically, use \fB\-g\fR.
153 .TP
154 .B \-r, \-\-reverse
155 Reverse the result of comparison, so that lines with greater key
156 values appear earlier in the output instead of later.
157 .TP
158 .B \-M
159 An initial string, consisting of any amount of whitespace, followed
160 by three letters abbreviating a month name, is folded to upper
161 case and compared in the order `JAN' < `FEB' < ... < `DEC'.
162 Invalid names compare low to valid names.
163 .SS Other Options
164 .TP
165 .B +\fIPOS1\fR[\fB-\fIPOS2\fR]
166 The obsolete, traditional option for specifying a sort field.  The
167 field consists of the line between \fIPOS1\fR and up to but \fBnot
168 including\fR \fIPOS2\fR (or the end of the line if \fIPOS2\fR is omitted).
169 Fields and character positions are numbered starting with 0.
170 .TP
171 .B \-k \fIPOS1\fR[\fB,\fIPOS2\fR]
172 .B \-K \fIPOS1\fR[\fB,\fIPOS2\fR]
173 The recommended, POSIX, option for specifying a sort field.  The
174 field consists of the line between \fIPOS1\fR and \fIPOS2\fR (or the end of
175 the line, if \fIPOS2\fR is omitted), \fBinclusive\fR.  Fields and character
176 positions are numbered starting with 1.
177 .TP
178 .B \-o \fIOUTFILE
179 Write output to OUTFILE instead of standard output.  If \fIOUTFILE\fR is one
180 of the input files, \fBsort\fR copies it to a temporary file before sorting
181 and writing the output to \fIOUTFILE\fR
182 .TP
183 .B \-t \fISEPARATOR
184 Use character \fISEPARATOR\fR as the field separator when finding the
185 sort keys in each line.  By default, fields are separated by the
186 empty string between a non-whitespace character and a whitespace
187 character.  That is, given the input line `foo bar', \fBsort\fR
188 breaks it into fields  `foo' and  `bar'.  The field separator is
189 not considered to be part of either the field preceding or the
190 field following.
191 .TP
192 .B \-u
193 For the default case or the \fB\-m\fR option, only output the first of
194 a sequence of lines that compare equal.  For the \fB\-c\fR option,
195 check that no pair of consecutive lines compares equal.
196 .TP
197 .B \-z
198 Treat the input as a set of lines, each terminated by a <NUL>
199 (zero byte character) instead of a <LF> (Line Feed)
200 This option can be useful in conjunction with `perl \-0' or find \-print0'
201 and `xargs \-0' which do the same in order to reliably 
202 handle arbitrary pathnames (even those which contain Line Feed characters)
203 .TP
204 .B -S \fISIZE\fB
205 Use a buffer of \fISIZE\fB KB.  Different units may be specified by
206 specifying multiplier suffix (see below).
207 .TP
208 .B \-T \fITEMPDIR
209 Use \fITEMPDIR\fR as the directory for temporary files. This option
210 overrides the \fBTMPDIR\fR environment variable.  If multiple \fB\-T\fR
211 options are specified, each directory is used and may improve performance on
212 huge sorts/merges.
213 .TP
214 .B \-\-help
215 Print a usage message on standard output and exit successfully.
216 .TP
217 .B \-\-version
218 Print version information on standard output then exit successfully.
219 .SH EXAMPLES
220 \fB\(bu\fR Sort in descending (reverse) numeric order.
221 .sp
222 .RS
223 .nf
224 sort \-nr
225 .fi
226 .RE          
227 .sp
228 \fB\(bu\fR Sort alphabetically, omitting the first and second fields.  This
229 uses a single key composed of the characters beginning at the start of field
230 three and extending to the end of each line.
231 .sp
232 .RS
233 .nf
234 sort \-k3
235 .fi
236 .RE
237 .sp
238 \fB\(bu\fR Sort numerically on the second field and resolve ties by sorting
239 alphabetically on the third and fourth characters of
240 field five.  Use `:' as the field delimiter.
241 .sp
242 .RS
243 .nf
244 sort \-t : -k 2,2n \-k 5.3,5.4
245 .fi
246 .RE
247 .sp
248 Note that if you had written `\-k 2' instead of `-k 2,2' \fBsort\fR
249 would have used all characters beginning in the second field and extending
250 to the end of the line as the primary \fBnumeric\fR key.  For the large
251 majority of applications, treating keys spanning more than one field as
252 numeric will not do what you expect.
253
254 Also note that the `n' modifier was applied to the field-end
255 specifier for the first key.  It would have been equivalent to
256 specify `\-k 2n,2' or `\-k 2n,2n'.  All modifiers except `b' apply
257 to the associated \fBfield\fR, regardless of whether the modifier
258 character is attached to the field-start and/or the field-end part
259 of the key specifier.
260
261 \fB\(bu\fR Sort the password file on the fifth field and ignore any leading
262 white space.  Sort lines with equal values in field five on the
263 numeric user ID in field three.
264 .sp
265 .RS
266 .nf
267 sort \-t : \-k 5b,5 \-k 3,3n /etc/passwd
268 .fi
269 .RE
270 .sp
271 An alternative is to use the global numeric modifier \fB\-n\fR.
272 .sp
273 .RS
274 .nf
275 sort \-t : \-n \-k 5b,5 \-k 3,3 /etc/passwd
276 .fi
277 .RE
278 .sp
279 \fB\(bu\fR Generate a tags file in case insensitive sorted order.
280 .sp
281 .RS
282 .nf
283 find src \-type f \-print0 | sort \-t / \-z \-f |
284 xargs \-0 etags \-\-append
285 .fi
286 .RE
287 .sp
288 The use of `\-print0', `\-z', and `\-0' in this case mean that
289 pathnames that contain Line Feed characters will not get broken up
290 by the sort operation.
291
292 Finally, to ignore both leading and trailing white space, you
293 could have applied the `b' modifier to the field-end specifier for
294 the first key,
295 .sp
296 .RS
297 .nf
298 sort \-t : \-n \-k 5b,5b \-k 3,3 /etc/passwd
299 .fi
300 .RE
301 .sp
302 or by using the global \fB\-b\fR modifier instead of \fB\-n\fR and an
303 explicit `n' with the second key specifier.
304 .sp
305 .RS
306 .nf
307 sort \-t : -b \-k 5,5 \-k 3,3n /etc/passwd
308 .fi
309 .RE
310 .SH MULTIPLIERS
311 Numbers may be followed by a size letter to specify a multiple of that
312 size, and a \fBB\fR to select normal bytes or a \fBD\fR to select
313 decimal "commercial" bytes.  For example `1KB' is equal to `1024' and
314 `1KD' is equal to `1000'.  Exceptions are \fBb\fR (512 bytes), \fBc\fR (1
315 byte), and \fBw\fR (which should never be used - it means 2 in System V and
316 4 in 4.2BSD), which can't be followed by a \fBB\fR or \fBD\fR.
317 .TP
318 .B k
319 kilo: 2^10 = 1024 for normal bytes, or 10^3 = 1000 for decimal bytes
320 .TP
321 .B M
322 Mega: 2^20 = 1,048,576 or 10^6 = 1,000,000
323 .TP
324 .B G
325 Giga: 2^30 = 1,073,741,824 or 10^9 = 1,000,000,000
326 .TP
327 .B T
328 Tera: 2^40 = 1,099,511,627,776 or 10^12 = 1,000,000,000,000
329 .TP
330 .B P
331 Peta: 2^50 = 1,125,899,906,842,624 or 10^15 = 1,000,000,000,000,000
332 .TP
333 .B E
334 Exa: 2^60 = 1,152,921,504,606,846,976 or 10^18 = 1,000,000,000,000,000,000
335 .TP
336 .B Z
337 Zetta: 2^70 = 1,180,591,620,717,411,303,424 or 10^21 =
338 1,000,000,000,000,000,000,000
339 .TP
340 .B Y
341 Yotta: 2^80 = 1,208,925,819,614,629,174,706,176 or 10^24 =
342 1,000,000,000,000,000,000,000,000
343 .SH COMPATIBILITY
344 Historical (BSD and System V) implementations of \fBsort\fR have
345 differed in their interpretation of some options, particularly 
346 .BR \-b ", " \-f ", and " \-n .
347 GNU sort follows the POSIX behavior, which is usually (but not always!) like
348 the System V behavior.  According to POSIX, \fB\-n\fR no longer implies
349 \fB-b\fR.  For consistency, \fB\-M\fR has been changed in the same way.
350 This may affect the meaning of character positions in field specifications
351 in obscure cases.  The only fix is to add an explicit \fB\-b\fR.
352 .SH LOCALES
353 .TP
354 .B LC_COLLATE
355 Specifies the character collating sequence used to realize all
356 comparisions, unless specified otherwise.
357 .TP
358 .B LC_CTYPE
359 Modifies the behaviour of the
360 .BR \-b ", " \-d ", " \-f " and "\-i
361 output ordering options.
362 .TP
363 .B LC_NUMERIC
364 Specifies the radix character as well as the thousands separator.
365 .TP
366 .B LC_TIME
367 Determines the month spellings, affecting \fB\-M\fR.
368 .SH NOTES
369 Report bugs to bug-textutils@gnu.org.
370 .br
371 Man page by Ragnar Hojland Espinosa <ragnar@ragnar-hojland.com>