z/OS UNIX System Services User's Guide
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Using sorting keys — an example

z/OS UNIX System Services User's Guide
SA23-2279-00

By default, sort sorts according to all the information in the record, in the order given in the record. Since the name of the comic book is the first thing on the line, the output is sorted according to comic book name. But suppose that you want to sort according to some different piece of information. For example, suppose you want to sort by date of publication. You can do this by specifying sorting keys.

A sorting key tells sort to look at specific fields in a record, instead of looking at each record as a whole. A sorting key also tells what kind of information is stored in a particular field (for example, an ordinary word, a number, or a month) and how that information should be sorted (in ascending or descending order).

A sorting key can refer to one or more fields. Fields are specified by number. The first field in a record is field number 1, the field after the first separator character is field number 2, and so on. In the comic book list, the month is field number 3, and the year is field number 4.

A single sort command can have several sorting keys. The most important sorting key is given first; less important sorting keys follow. Let us look at an example that sorts by year and then by month within a year. Therefore, the first sorting key refers to the year field, and the second to the month field. To specify a sorting key, use the -k option. This option has the following format:
-k start_field[.char1] [opts] [,end_field[.char2] [opts]]
where start_field, end_field, char1, and char2 are all integers.
  • start_field indicates which field in the input record contains the start of the sorting key.
  • char1 indicates which character in that field is the first character of the key. Omitting char1 means the key begins with the first character of the starting field.
In our example, the first sorting key (referring to the year) has a start_field value of 4 (since the year is field 4). We do not need to specify char1, since we want to start the key with the first character of the year field.
The options, opts, are specified with letters; they identify the type of data in the specified field and tell how to sort it. Some of the possible options and their meanings are:
d
Indicates that the field contains uppercase, lowercase, or mixed-case letters, letters and digits, or digits. sort sorts the field in dictionary order, ignoring all other characters.
M
Indicates that the field contains the name of a month. sort looks only at the first three characters of the name, so January, JAN, and jan are all equal.
n
Indicates that the field contains an integer (positive or negative).

Putting an r after any of these letters tells sort to sort in reverse order (from highest to lowest rather than lowest to highest). For example, Mr means to sort in the order December, November, October, and so on.

In our example the sorting key based on the year uses n. Thus, the sorting key for the year field (4) in the file comics.lst is:
-k 4n
The second sorting key in the example refers to the month field (3). This key has the form:
-k 3M
A sort command that uses sorting keys needs to know which character separates the record fields. You can specify this with the option -t followed by the separator character. The example uses -t:. Therefore, the full sort command is:
sort -t: -k4n -k3M  comics.lst >sorted.lst
The file to be sorted comes after the various options. This is the order that you must use. The redirection construct can come anywhere on the line, but is usually put at the end.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014