blob: eb0d7f46db69d180bddc2345c8da101462f621c3 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH SORT 1
2.SH NAME
3sort \- sort and/or merge files
4.SH SYNOPSIS
5.B sort
6[
7.BI -cmuMbdf\&inrwt x
8]
9[
10.BI + pos1
11[
12.BI - pos2
13] ...
14] ...
15[
16.B -k
17.I pos1
18[
19.I ,pos2
20]
21] ...
22[
23.B -o
24.I output
25]
26[
27.B -T
28.I dir
29\&...
30]
31[
32.I option
33\&...
34]
35[
36.I file
37\&...
38]
39.SH DESCRIPTION
40.I Sort\^
41sorts
42lines of all the
43.I files
44together and writes the result on
45the standard output.
46If no input files are named, the standard input is sorted.
47.PP
48The default sort key is an entire line.
49Default ordering is
50lexicographic by runes.
51The ordering is affected globally by the following options,
52one or more of which may appear.
53.TP
54.B -M
55Compare as months.
56The first three
57non-white space characters
58of the field
59are folded
60to upper case
61and compared
62so that
63.L JAN
64precedes
65.LR FEB ,
66etc.
67Invalid fields
68compare low to
69.LR JAN .
70.TP
71.B -b
72Ignore leading white space (spaces and tabs) in field comparisons.
73.TP
74.B -d
75`Phone directory' order:
76only letters,
77accented letters,
78digits and white space
79are significant in comparisons.
80.TP
81.B -f
82Fold lower case
83letters onto upper case.
84Accented characters are folded to their
85non-accented upper case form.
86.TP
87.B -i
88Ignore characters outside the
89.SM ASCII
90range 040-0176
91in non-numeric comparisons.
92.TP
93.B -w
94Like
95.BR -i ,
96but ignore only tabs and spaces.
97.TP
98.B -n
99An initial numeric string,
100consisting of optional white space,
101optional plus or minus sign,
102and zero or more digits with optional decimal point,
103is sorted by arithmetic value.
104.TP
105.B -g
106Numbers, like
107.B -n
108but with optional
109.BR e -style
110exponents, are sorted by value.
111.TP
112.B -r
113Reverse the sense of comparisons.
114.TP
115.BI -t x\^
116`Tab character' separating fields is
117.IR x .
118.PP
119The notation
120.BI + "pos1\| " - pos2\^
121restricts a sort key to a field beginning at
122.I pos1\^
123and ending just before
124.IR pos2 .
125.I Pos1\^
126and
127.I pos2\^
128each have the form
129.IB m . n\f1,
130optionally followed by one or more of the flags
131.BR Mbdfginr ,
132where
133.I m\^
134tells a number of fields to skip from the beginning of the line and
135.I n\^
136tells a number of characters to skip further.
137If any flags are present they override all the global
138ordering options for this key.
139A missing
140.BI \&. n\^
141means
142.BR \&.0 ;
143a missing
144.BI - pos2\^
145means the end of the line.
146Under the
147.BI -t x\^
148option, fields are strings separated by
149.IR x ;
150otherwise fields are
151non-empty strings separated by white space.
152White space before a field
153is part of the field, except under option
154.BR -b .
155A
156.B b
157flag may be attached independently to
158.IR pos1
159and
160.IR pos2.
161.PP
162The notation
163.B -k
164.IR pos1 [, pos2 ]
165is how POSIX
166.I sort
167defines fields:
168.I pos1
169and
170.I pos2
171have the same format but different meanings.
172The value of
173.I m\^
174is origin 1 instead of origin 0
175and a missing
176.BI \&. n\^
177in
178.I pos2
179is the end of the field.
180.PP
181When there are multiple sort keys, later keys
182are compared only after all earlier keys
183compare equal.
184Lines that otherwise compare equal are ordered
185with all bytes significant.
186.PP
187These option arguments are also understood:
188.TP \w'\fL-z\fIrecsize\fLXX'u
189.B -c
190Check that the single input file is sorted according to the ordering rules;
191give no output unless the file is out of sort.
192.TP
193.B -m
194Merge; assume the input files are already sorted.
195.TP
196.B -u
197Suppress all but one in each
198set of equal lines.
199Ignored bytes
200and bytes outside keys
201do not participate in
202this comparison.
203.TP
204.B -o
205The next argument is the name of an output file
206to use instead of the standard output.
207This file may be the same as one of the inputs.
208.TP
209.BI -T dir
210Put temporary files in
211.I dir
212rather than in
213.BR /tmp .
214.ne 4
215.SH EXAMPLES
216.TP
217.L sort -u +0f +0 list
218Print in alphabetical order all the unique spellings
219in a list of words
220where capitalized words differ from uncapitalized.
221.TP
222.L sort -t: +1 /adm/users
223Print the users file
224sorted by user name
225(the second colon-separated field).
226.TP
227.L sort -umM dates
228Print the first instance of each month in an already sorted file.
229Options
230.B -um
231with just one input file make the choice of a
232unique representative from a set of equal lines predictable.
233.TP
234.L
235grep -n '^' input | sort -t: +1f +0n | sed 's/[0-9]*://'
236A stable sort: input lines that compare equal will
237come out in their original order.
238.SH FILES
239.BI /tmp/sort. <pid>.<ordinal>
240.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000241.B \*9/src/cmd/sort.c
rsccfa37a72004-04-10 18:53:55 +0000242.SH SEE ALSO
243.IR uniq (1),
244.IR look (1)
245.SH DIAGNOSTICS
246.I Sort
247comments and exits with non-null status for various trouble
248conditions and for disorder discovered under option
249.BR -c .
250.SH BUGS
251An external null character can be confused
252with an internally generated end-of-field character.
253The result can make a sub-field not sort
254less than a longer field.
255.PP
256Some of the options, e.g.
257.B -i
258and
259.BR -M ,
260are hopelessly provincial.