Open System Services Shell and Utilities Reference Manual (G06.29+, H06.08+, J06.03+)

User Commands (s) sort(1)
This order follows from the fact that in the ASCII collating sequence, symbols (such as
%) precede uppercase letters, and all uppercase letters precede the lowercase letters. If
you are using a different collating order, your results may be different.
2. To group lines that contain uppercase and special characters with similar lowercase lines,
and remove duplicate lines, enter:
sort -d -f -u fruits
The -u ag tells sort to remove duplicate lines, making each line of the file unique. This
displays:
apple
%%banana
orange
Persimmon
Note that not only was the duplicate apple removed, but banana and ORANGE were
removed as well. The -d ag told sort to ignore symbols, so %%banana and banana
were considered to be duplicate lines and banana was removed. The -f ag told sort not
to differentiate between uppercase and lowercase, so ORANGE and orange were con-
sidered to be duplicate lines and ORANGE was removed.
When the -u ag is used with input that contains nonidentical lines that are considered
by sort (due to other flags) to be duplicates, there is no way to predict which lines sort
will keep and which it will remove.
3. To sort as in Example 2, but remove duplicates unless capitalized or punctuated
differently, enter:
sort -u -k 1df -k 1 fruits
Flags appearing between sort key specifiers apply only to the specifier preceding them.
There are two sorts specified in this command line. -k 1df specifies the first sort, of the
same type done with -d -f in Example 3. Then -k 1 performs another comparison to dis-
tinguish lines that are not actually identical. This prevents -u, which applies to both sorts
because it precedes the first sort key specifier, from removing lines that are not exactly
identical to other lines.
Given the fruits file shown in Example 1, the added -k 1 distinguishes %%banana from
banana and ORANGE from orange. However, the two instances of apple are exactly
identical, so one of them is deleted.
apple
%%banana
banana
ORANGE
orange
Persimmon
4. To specify a new field separator, enter:
sort -t : -k 2 vegetables
This sorts vegetables, comparing the text that follows the first colon on each line. -t :
tells sort that colons separate fields. -k 2 tells sort to ignore the first field and to com-
pare from the start of the second field to the end of the line. If vegetables contains:
yams:104
turnips:8
527188-021 Hewlett-Packard Company 855