Hints and tips when using the UNIX sort command
The user interface for the UNIX sort command is challenging. AIX delivers the UNIX sort command, so the AIX sort command has the same challenging user interface as other UNIX variants. One redeeming feature of the UNIX sort command is that it is delivered in the bos.rte.commands fileset, so it is found on every AIX installation that has not been tampered with.
This web page is an attempt to provide the reader with enough information to use the sort command effectively for everyday tasks.
This web page is meant to supplement, not replace, existing documentation:
The AIX V5.3 sort command documentation says:
"The default field separator is a sequence of one or more consecutive blank characters. However, these blank characters are considered to be a part of the following field for sorting purposes. You can specify the -b option to ignore these leading blank characters."
The fact that leading blank and tab characters are included (by default) in each key field is one of the more challenging aspects of the sort command user interface. When specifying a key field, it is counterintuitive to count leading blanks and tabs as part of the key field. To avoid difficulty, the b modifier or -b flag should generally be used.
The AIX V5.3 sort command documentation says:
"The value of the Modifier variable can be b, d, f, i, n, or r. The modifiers are equivalent to the flags of the same letter."
The AIX V5.3 sort command documentation would be clearer if the following sentence were added:
"If a modifier is used to alter a key field definition, then additional modifiers can be specified, but any flags which would normally modify the key field definition are ignored."
So if the b modifier is used in a key field definition, any flags (eg, -n) which would normally modify the key definition are ignored.
AIX APAR IZ49161 (SORT COMMAND DOCUMENTATION NEEDS CLARITY)
was opened on 4-14-2009 to add the above clarifications to the sort command documentation.
The AIX V5.3 sort command documentation says:
"When numbering columns within fields, the blank characters in a default field separator are counted as part of the following field. Leading blanks are not counted as part of the first field, and field separator characters specified by the -t flag are not counted as parts of fields. Leading blank characters can be ignored using the -b flag."
The AIX V5.3 sort command documentation would be clearer if it said:
"When numbering the columns within a field, columns are counted beginning in the blank and tab characters preceding the field. The b modifier or -b flag causes leading blank and tab characters to be ignored. A field separator character specified by the -t flag is never counted as part of a field."
The AIX V5.3 sort command documentation says:
"If two lines sort equally on all sort keys, the entire lines are then compared based upon the collation order in the current locale."
Many seemingly inexpicable results from sort can be understood if/when this aspect of sort behavior is considered.
The AIX V5.3 sort command documentation says:
"When using sort keys, the sort command first sorts all lines on the contents of the first sort key. Next, all the lines whose first sort keys are equal are sorted upon the contents of the second sort key, and so on.
The sort key includes all characters beginning with the field specified by the FStart variable and the column specified by the CStart variable and ending with the field specified by the FEnd variable and the column specified by the CEnd variable. If Fend is not specified, the last character of the line is assumed. If CEnd is not specified the last character in the FEnd field is assumed. "
When a key field is defined and no ending field or column is specified, the key field continues to the end of the line. If the key is unique (no two lines have the same key), then any subsequent keys on the sort command line will be ignored. When using multiple keys, all key fields except the last should generally have a defined end or should fall in columns to the left of earlier key fields.
Here are some examples of sorting a file according to hdisk number:
It is important to use the b modifier when the hdisk field is not preceded by the same number of blanks on every line, but there is an exception - when the hdisk number falls in the same column on every line but does not fall in the same field on every line:
Above, the column in which the hdisk number starts is specified as an absolute offset from the beginning of the line. Please note that the column specified for a field is not required to fall within the field. In this case, the column specified for field 1 falls within field 2 or field 3 depending on the file line being considered.
As mentioned earlier, do not mix use of modifiers and flags. The following examples work properly:
The following examples do not work properly:
The last example is particularly difficult to explain until one realizes that sort is behaving as if no key field was specified:
The AIX V5.3 sort command documentation says:
"Numeric sorting of a field containing any nonnumeric character gives unpredictable results."
With sort -b -k3.6n /tmp/sortdsk1, the -b flag is ignored because the n modifier is used. When -b is ignored, the specified key field contains nonnumeric characters, so the sort command ignores the key field definition and sorts as if no key field was specified.
The contents of this web page solely reflect the personal views of the authors and do not necessarily represent the views, positions, strategies or opinions of IBM or IBM management. Please use the
Add Comment link at the bottom of the page to provide feedback. Note: Until you sign up and log in (using links in the upper right corner of this web page), you will not see the
Add Comment link and you can not add a comment.