Open System Services Shell and Utilities Reference Manual (G06.25+, H06.03+)
Table Of Contents
sort(1) OSS Shell and Utilities Reference Manual
The -u flag 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 flag told sort to ignore symbols, so %%banana and banana
were considered to be duplicate lines and banana was removed. The -f flag 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 flag 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
potatoes:15
carrots:104
green beans:32
radishes:5
lettuce:15
then sort -t : -k 2 vegetables displays:
carrots:104
8−48 Hewlett-Packard Company 527188-003