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

sort(1) OSS Shell and Utilities Reference Manual
The -u ag tells sort to remove duplicate lines, making each line of the le 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 ags) 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 speciers apply only to the specier preceding them.
There are two sorts specied in this command line. -k 1df species the rst 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 rst sort key specier, from removing lines that are not exactly
identical to other lines.
Given the fruits le 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 eld separator, enter:
sort -t : -k 2 vegetables
This sorts vegetables, comparing the text that follows the rst colon on each line. -t :
tells sort that colons separate elds. -k 2 tells sort to ignore the rst eld and to com-
pare from the start of the second eld to the end of the line. If vegetables contains:
yams:104
turnips:8
potatoes:15
carrots:104
green beans:32
radishes:5
lettuce:15
then sort -t : -k 2 vegetables displays:
carrots:104
848 Hewlett-Packard Company 527188-003