sort(1) sort(1)NAME sort - sort or merge files SYNOPSIS sort [-c] [-m] [-u] [-o output] [-y[kmem]] [-zrecsz] [-d] [-f] [-i] [-M] [-n] [-r] [-b] [-t x] [+pos1 [-pos2]] [file...] DESCRIPTION sort sorts lines of all the named files together and writes the result on the standard output. The standard input is read if - is used as a filename or no input files are named. Comparisons are based on one or more sort keys extracted from each line of input. By default there is one sort key (the entire input line) and ordering is lexicographic by bytes in machine collating sequence. FLAG OPTIONS The following flag options alter the default behavior: -c Check that the input file is sorted according to the ordering rules; give no output unless the file is out of sort. -m Merge only because the input files are already sorted. -u Suppress all but one (unique) line in each set of lines having equal keys. -o output Place the output in the file output instead of in the standard output. This file may be the same as one of the inputs. There may be optional blanks between -o and output. -ykmem Sort using a specified amount of kilobytes of memory (kmem) The amount of main memory used by the sort has a large impact on its performance. Sorting a small file in a large amount of memory is a waste. If this flag option is omitted, sort begins using a system default memory size and continues to use more space as needed. If this flag option is presented with the value, kmem, sort starts using that number of kilobytes of memory, unless the administrative minimum or maximum is violat- ed, in which case the corresponding extremum is used. Thus, -y0 is guaranteed to start with minimum memory. By convention, -y (with no argument) starts with max- imum memory. -zrecsz April, 1990 1
sort(1) sort(1)Record in the sort phase the size of the longest line read so buffers can be allocated during the merge phase. If the sort phase is omitted via the -c or -m flag options, a popular system default size is used. Lines longer than the buffer size causes sort to ter- minate abnormally. Supplying the actual number of bytes (or some larger value) in the longest line to be merged prevents abnormal termination. The following flag options override the default ordering rules: -d Use ``dictionary'' order. Only letters, digits, and blanks (spaces and tabs) are significant in comparis- ons. -f Fold lowercase letters into uppercase. -i Ignore characters outside the ASCII range 040-0176 in non-numeric comparisons. -M Compare as months. The first three nonblank characters of the field are folded to uppercase and compared so that JAN < FEB < ... < DEC. Invalid fields compare low to JAN. The -M flag option implies the -b flag option (see later in this section). -n Sort by arithmetic value an initial numeric string, consisting of optional blanks, an optional minus sign, and zero or more digits with optional decimal point. The -n flag option implies the -b flag option (as described later). Note that the -b flag option is only effective when restricted sort-key specifications are in effect. -r Reverse the sense of comparisons. When ordering flag options appear before restricted sort key specifications, the requested ordering rules are applied globally to all sort keys. When attached to a specific sort key (as described later), the specified ordering flag op- tions override all global ordering flag options for that key. The notation +pos1 -pos2 restricts a sort key to one begin- ning at pos1 and ending just before pos2. The characters at positions pos1 and pos2 are included in the sort key (pro- vided that pos2 does not precede pos1). A missing -pos2 designates the end of the line. Specifying pos1 and pos2 involves the notion of a field, a minimal sequence of characters followed by a field separator 2 April, 1990
sort(1) sort(1)or a newline. By default, the first blank (space or tab) of a sequence of blanks acts as the field separator. All blanks in a sequence of blanks are considered to be part of the next field; for example, all blanks at the beginning of a line are considered to be part of the first field. The treatment of field separators can be altered by using the flag options. -b Ignore leading blanks when determining the beginning and ending positions of a restricted sort key. If the -b flag option is specified before the first +pos1 ar- gument, it is applied to all +pos1 arguments. Other- wise, the b flag may be attached independently to each +pos1 or -pos2 argument (as shown later). -tx Use x as the field-separator character; x is not con- sidered to be part of a field (although it may be in- cluded in a sort key). Each occurrence of x is signi- ficant; for example, xx delimits an empty field. Both of the arguments, pos1 and pos2, have the form m.n op- tionally followed by one or more of the flags b, d, f, i, n, r, where m specifies the number of fields to skip from the beginning of the line and n specifies the number of charac- ters to skip beyond. Thus, a starting position specified by +m.n is interpreted to mean the n+1st character in the m+1st field. A missing .n means .0, indicating the first charac- ter of the m+1st field. If the b flag is in effect, n is counted from the first nonblank in the m+1st field; +m.0b refers to the first nonblank character in the m+1st field. A last position specified by -m.n is interpreted to mean the nth character (including separators) after the last charac- ter of the mth field. A missing .n means .0, indicating the last character of the mth field. If the b flag is in ef- fect, n is counted from the last leading blank in the m+1st field; -m.1b refers to the first nonblank in the m+1st field. When there are multiple sort keys, later keys are compared only after all earlier keys compare equal. Lines that oth- erwise compare equal are ordered with all bytes significant. EXAMPLES To sort the contents of infile with the second field as the sort key, use the command sort +1 -2 infile To sort, in reverse order, the contents of infile1 and in- file2, placing the output in outfile and using the first character of the second field as the sort key, use the com- April, 1990 3
sort(1) sort(1)mand sort -r -o outfile +1.0 -1.2 infile1 infile2 To sort, in reverse order, the contents of infile1 and in- file2 using the first nonblank character of the second field as the sort key, use the command sort -r +1.0b -1.1b infile1 infile2 To print the password file (passwd(4)) sorted by the numeric user ID (the third colon-separated field), use the command sort -t: +2n -3 /etc/passwd To print the lines of the already sorted file infile, suppressing all but the first occurrence of lines having the same third field (the flag options -um, with just one input file, make the choice of a unique representative from a set of equal lines predictable), use the command sort -um +2 -3 infile FILES /bin/sort /usr/tmp/stm??? SEE ALSO comm(1), join(1), rev(1), sortbib(1), tsort(1), uniq(1). DIAGNOSTICS sort comments and exits with nonzero status for various trouble conditions (for example, when input lines are too long), and for disorder discovered under the -c flag option. When a newline character is missing from the last line of an input file, sort appends one, prints a warning message, and continues. 4 April, 1990