2

The man page for sort’s -u option says:

with -c, check for strict ordering; without -c, output only the first of an equal run

Can someone translate this into basic English for me? The -uc option gives me the same output as only -c.

1

3 Answers 3

3

Strict ordering means that no value can be repeated. Compare the output of

printf "%s\n" 1 2 3 4 4 | sort -uc

with that of

printf "%s\n" 1 2 3 4 4 | sort -c
2

The -c option to sort causes sort to check whether the input data is sorted correctly, taking any other options into account. It will not perform any sorting. In some implementations of sort, -c may be written --check.

So sort -u -c would perform a check on the data and let you know whether the data is sorted and contains any duplicate entries. If it is sorted and contains no duplicate entries it won't say anything, but if there are duplicate entries or if the data is unsorted, it will produce a diagnostic message and exit with a non-zero exit status.

You may used this in the following way:

if sort -uC file; then
   echo 'file is sorted and contains no duplicate entries'
else
   echo 'file is unsorted or contains duplicate entries'
fi

The -C option does the same thing as -c but stops sort from producing any output. -C may sometimes be written --check=quiet or --check=silent.

1

Here is how the extended sort manual (info coreutils 'sort invocation') explains it:

‘--unique’

Normally, output only the first of a sequence of lines that compare equal. For the --check (-c or -C) option, check that no pair of consecutive lines compares equal.

I.e. combining -c (check) with -u (unique) will, quite logically, check that there is no duplicates in the input.

For example:

%echo -e "a\na\nb\nc"| sort -c && echo OK
OK

%echo -e "a\na\nb\nc"| sort -uc && echo OK
sort: -:2: disorder: a

While the input is sorted (a->a->b->c), there are duplicates, so -uc will fail.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .