1.\" Copyright (c) 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the Institute of Electrical and Electronics Engineers, Inc. 6.\" 7.\" %sccs.include.redist.roff% 8.\" 9.\" @(#)sort.1 8.1 (Berkeley) 06/10/93 10.\" 11.Dd 12.Dt SORT 1 13.Os 14.Sh NAME 15.Nm sort 16.Nd sort or merge text files 17.Sh SYNOPSIS 18.Nm sort 19.Op Fl mubdfinrtx 20.Oo 21.Cm \(pl Ns Ar pos1 22.Op Fl Ns Ar pos2 23.Oc 24.Ar ... 25.Op Fl o Ar output 26.Op Fl T Ar directory 27.Op Ar file 28.Ar ... 29.Sh DESCRIPTION 30The 31.Nm sort 32utility 33sorts text files by lines. 34Comparisons are based on one or more sort keys (or fields) extracted 35from each line of input, and are performed 36lexicographically. By default, if keys are not given, 37.Nm sort 38regards each input line as a single field. 39.Pp 40The following options are available: 41.Bl -tag -width indent 42.It Fl c 43Check that the single input file is sorted lexicographically. 44If the file is not sorted, 45.Nm sort 46sorts it and writes the sorted output to the standard output or the 47filename specified by the 48.Fl o 49option. 50.It Fl m 51Merge only; the input files are assumed to be pre-sorted. 52.It Fl o Ar output 53The argument given is the name of an 54.Ar output 55file to 56be used instead of the standard output. 57This file 58can be the same as one of the input files. 59.It Fl T Ar directory 60The argument 61.Ar directory 62is used for creating temporary files. 63.It Fl u 64Unique: suppress all but one in each set of lines 65having equal keys. 66If used with the 67.Fl c 68option, 69check that there are no lines with duplicate keys. 70.El 71.Pp 72The following options override the default ordering rules. 73When ordering options appear independent of key field 74specifications, the requested field ordering rules are 75applied globally to all sort keys. 76.\" When attached to a 77.\" specific key 78.\" (see 79.\" .Fl k ) , 80.\" the specified ordering options override 81.\" all global ordering options for that key. 82.Bl -tag -width indent 83.It Fl d 84Only blank space and alphanumeric characters 85.\" according 86.\" to the current setting of LC_CTYPE 87are used 88in making comparisons. 89.It Fl f 90Considers all lowercase characters that have uppercase 91equivalents to be the same for purposes of 92comparison. 93.It Fl i 94Ignore all non-printable characters. 95.It Fl n 96An initial numeric string, consisting of optional 97blank space, optional minus sign, and zero or more 98digits (including decimal point) 99.\" with 100.\" optional radix character and thousands 101.\" separator 102.\" (as defined in the current locale), 103is sorted by arithmetic value. 104The 105.Fl n 106option implies 107the 108.Fl b 109option. (See below.) 110Note that the 111.Fl b 112option 113is only effective when key fields have been specified 114and that 115.Fl \&0 116is considered equal to zero. 117.optional It Fl r 118Reverse the sense of comparisons. 119.El 120.Pp 121The treatment of field separators can be altered using the 122options: 123.Bl -tag -width indent 124.It Fl b 125Leading blank spaces are ignored when determining the starting 126ending positions of a restricted sort key. 127If the 128.Fl b 129option is specified before the first 130.Cm \(pl Ns Ar pos1 131argument, it shall be applied to all 132.Cm \(pl Ns Ar pos1 133arguments. 134Otherwise, the 135.Fl b 136option can be 137attached independently to each 138.Cm \(pl Ns Ar pos1 139or 140.Fl Ar pos2 141argument (see below). 142.It Fl t Ar char 143.Ar Char 144is used as the field separator character; 145.Ar char 146is not considered to be part of a field (although it 147can be included in a sort key). 148Each occurrence of 149.Ar char 150is significant (for example, 151.Dq Ar charchar 152delimits an empty field). 153If 154.Fl t 155is not specified, 156blank space characters are used as default field 157separators. 158.It Cm \(pl Ns Ar pos1 159Designates the start position of a key field. 160.It Fl Ns Ar pos1 161Designates the end position of a key field. 162.El 163.Pp 164The following operands are available: 165.Bl -tag -width indent 166.Ar file 167The pathname of a file to be sorted, merged, or checked. 168If no file 169operands are specified, or if 170a file operand is 171.Fl , 172the standard input is used. 173.Pp 174A field is 175defined as a minimal sequence of characters followed by a 176field separator or a newline character. 177By default, the first 178blank space of a sequence of blank spaces acts as the field separator. 179All blank spaces in a sequence of blank spaces are considered 180to be part of the next field; for example, all blank spaces at 181the beginning of a line are considered to be part of the 182first field. 183.Pp 184Fields are specified 185by the 186.Cm \(pl Ns Ar pos1 187and 188.Fl Ar pos2 189arguments. A missing 190.Cm \(pl Ns Ar pos1 191argument defaults to the beginning of a line. 192A missing 193.Fl Ar pos2 194argument defaults to the end of a line. 195.Pp 196The arguments 197.Cm \(pl Ns Ar pos1 198and 199.Fl Ar pos2 200have the form 201.Em m.n 202followed by one or more of the options 203.Fl b , d , f , i , 204.Fl n , r . 205A 206.Cm \(pl Ns Ar pos1 207position specified by 208.Em m.n 209is interpreted to 210mean the 211.Em n Ns th 212character in the 213.Em m Ns \(pl1th 214field. 215A missing 216.Em \&.n 217means 218.Ql \&.0 , 219indicating the first character of the 220.Em m Ns \(pl1th 221field. 222If the 223.Fl b 224option is in effect, 225.Em n 226is counted from the first 227non-blank character in the 228.Em m Ns \(pl1th 229field; 230.Em m Ns \&.0b 231refers to the first 232non-blank character in the 233.Em m Ns \(pl1th 234field. 235.Pp 236A 237.Fl Ar pos2 238position specified by 239.Em m.n 240is interpreted to mean 241the 242.Em n Ns th 243character (including separators) after the last 244character of the 245.Em m Ns th 246field. 247A missing 248.Em \&.n 249means 250.Ql \&.0 , 251indicating 252the last character of the 253.Em m Ns th 254field. 255If the 256.Fl b 257option 258is in effect, 259.Em n 260is counted from the last leading blank character in 261the 262.Em m Ns \(pl1th 263field; 264.Em m Ns \&.1b 265refers to the first non-blank character in the 266.Em m Ns \(pl1th 267field. 268.Sh FILES 269.Bl -tag -width Pa -compact 270.It Pa /var/tmp/stm*, /tmp/* 271Default temporary directories (in order of search). 272.El 273.Sh SEE ALSO 274.Xr comm 1 , 275.Xr uniq 1 , 276.Xr join 1 277.Sh DIAGNOSTICS 278.Sh BUGS 279Lines which are longer than 4096 are discarded and processing continues. 280.Sh HISTORY 281A 282.Nm 283command appeared in 284.At v6 . 285