Tools: MPW
Advanced Search
Apple Developer Connection
Member Login Log In | Not a Member? Support

MPW Command Reference


Sort

Tool

SYNTAX

Sort [file1] [file2]… [-b] [-check] [-d] [-f field1[,field2]…]
[-fs string] [-l] [-merge] [-o file] [-p] [-quote] [-r] [-stdin] [-t]
[-u] [-unique] [-x]

DESCRIPTION

The Sort tool sorts the text lines in the specified files. Normally lines are compared in their entirety and sorted either as text (the default) or as decimal numbers (-d). More usefully, however, lines can be compared by field (-f) to perform more complex sorting (for example, to sort a list of files and their sizes by size).

In addition to sorting text lines, you can also use this tool to

check whether the input text lines are already sorted (-check)

merge presorted input files (-merge)

INPUT

Standard input, unless you specify input files. Unlike other commands, Sort also allows you to compare standard input to one or more text files. To do this you specify -stdin.

OUTPUT

Standard output, unless you specify an output file (-o). In either case Sort concatenates the sorted input files and writes the result.

If you specify -check, Sort does not generate any output; instead the tool's status code indicates whether the input is already sorted.

STATUS

Sort can return the following status codes:

0

no errors; input files sorted (-check)

1

syntax error

2

any other error

4

out of memory

5

input files not sorted (-check)

PARAMETERS

file1 [file2]…

Specifies the input files to be sorted.

OPTIONS

-b

Skips leading spaces in each field.

-check

Checks whether the input files are already sorted according to the field specifications. Sort exits with a status of 0 if the files are already sorted and with a status of 5 if they are not.

-d

Sorts fields as decimal numbers. The numbers can be of arbitrary length.

-f field1[,field2]…

Specifies the text fields by which the tool sorts, where each field parameter is a string of characters separated by spaces, tabs, backspaces, or form feeds. To change these field separators, use -fs.

In comparing two text lines, Sort follows these rules:

The line whose fields are alphabetically or numerically (-d or -x) first is displayed first.

If a field exists in one line but not the other, the line containing the field is displayed first.

If neither line contains a specified field, Sort considers the lines equal.

Normally Sort considers each text line a field and thus sorts by entire lines. However, you can sort by other fields by using a field specification of the form:

[F] [.c] [-k | +n] [modifiers]

F

Specifies the actual field, where F is a nonnegative integer. The default setting (F = 0) specifies the entire line, field 1 typically specifies the first word, field 2 the second, and so on. For example, in the text line

And I choose never to stoop

field 0 is the entire line, field 1 is And, field 2 is I, and so on.

.c

Specifies the starting column in the field, where c is a nonnegative integer. The default setting (.c = .1) specifies the first column, .2 the second, and so on. For example, in the previous text line the letter d is in the third column (.c = .3).

In determining the starting column, Sort takes into account the different tab settings of the input files.

-k

Specifies the last column for the field, where k is a nonnegative integer. The default setting is infinity (∞).

Note
This does not mean that the field extends to the end of the line. Except for field 0, fields are always terminated by field separator characters. •

+n

Specifies the number of characters to be included in the field, counting from the starting column. The default setting is infinity (∞). (See the note under -k.)

modifiers

Specifies one or more letters that modify the default behavior set by the options for only the specified field.

r       Reverses -r
b       Reverses -b
q       Reverses -quote
d       Sets -d
x       Sets -x
t       Sets -t, the default
l       Sets -l
u       Sets -u

-fs string

Specifies the field separator characters, overriding the default separators (spaces, tabs, backspaces, and form feeds). Note that fields cannot cross newlines.

Note
This switch completely replaces the default set of separators with the specified set. •

-l

Converts alphabetic characters to lowercase before comparing them. Sort is normally case sensitive and considers lowercase letters to have a lower sorting value than uppercase letters, placing them last. Note that the original case of letters is preserved in the output text lines.

-merge

Merges the input files, without sorting the lines. For this option, Sort assumes that the input files are already sorted; if one or more of the input files has not been sorted, the output is not sorted.

-o file

Specifies the output file to be other than standard output. This option allows you to sort, though not to merge, a file "in place." (In other words, the output file can be one of the input files.)

-p

Writes progress and version information to diagnostic output.

-quote

Ignores field separators enclosed in single and double quotation marks. Using this option, characters preceded by the MPW Shell quotation character (∂) are properly escaped. Quotation marks themselves are ignored in comparisons, and sets of alternating quotation marks (such as ' " ' …stuff…' " ') can be nested to any depth. If one of a pair of quotation marks is missing, the field extends to the end of the line.

-r

Reverses the order of comparison. This causes Sort to display lines first that are alphabetically or numerically last (-d or -x).

-stdin

Sorts or merges standard input with the specified files. This option serves as a placeholder for standard input. (Remember that you can terminate standard input by pressing Command-Enter.)

-t

Sorts fields as text (the default).

-u

Converts characters to uppercase before comparing them. Normally Sort is case sensitive and considers uppercase letters to have a higher value than all lowercase letters, placing them first.

Using this option makes all letters equal. Note that the original case of letters is preserved in the output text lines.

-unique

Writes identical input lines only once.

-x

Sorts fields as hexadecimal numbers (uppercase or lowercase). Note that numbers can be of arbitrary length and that Sort ignores the leading dollar sign ($) or 0x.

EXAMPLES

Although you can use this tool to sort text lines alphabetically, Sort can also be used with field specifications to perform more complex sorting. For example, this list of filenames and sizes:

moo   812
lott  213
bar   315

can be sorted by size by using the command line

Sort SortSample -f 2 -d

This command line sorts field 2 of the input lines as decimal numbers to produce this output:

lott  213
bar   315
moo   812

Using -stdin, the following command line sorts the Able and Baker files along with standard input and writes the sorted text lines to the Output file:

Sort Able Baker -stdin -o Output

The following command line sorts the Frog file, according to two fields: the first field is treated as text and sorted in reverse order; the second field, which consists of eight characters, starting at the second column of the second field, is treated as a hexadecimal number.

Sort Frog -x -f '1tr, 2.2+8'

The next command line merges the specified files, treating lowercase letters as uppercase, and writes progress and version information.

Sort one two three infinity -merge -u -p

 
 


Last Updated July 2000