3

anyone could explain why "sort -nu" returns only 1 unique line in example below?

$ cat /tmp/test_
1e971713-31e4-62d0-a3d1-37c0610c10e5
1e97d59f-bc32-6126-b7b1-0b52ddd35273
1e971713-31e4-62d0-a3d1-37c0610c10e5
$ sort -nu /tmp/test_
1e971713-31e4-62d0-a3d1-37c0610c10e5
$

shouldn't there be two unique lines on output like on "sort -u"?

$ sort -u /tmp/test_
1e971713-31e4-62d0-a3d1-37c0610c10e5
1e97d59f-bc32-6126-b7b1-0b52ddd35273
$
3
  • 3
    Why are you using -n to sort non-numerical data? I'm assuming it stops comparing after the initial 1 (no more digits) and since all lines starts with 1, there is only one "unique" key. Can't say this is what happens for certain, but it behaves like it. – Kusalananda Jun 26 '19 at 21:16
  • @Kusalananda that certainly seems to be the case for the GNU implementation (as one can verify by adding the --debug option) – steeldriver Jun 26 '19 at 21:17
  • @steeldriver BSD sort on OpenBSD behaves the same, although --debug there shows the whole line as the key. I'm assuming it then uses strtol() to convert the keys to integers, which would return the initial 1 only. – Kusalananda Jun 26 '19 at 21:19
10

@Kusalananda is right.

https://www.gnu.org/software/coreutils/manual/html_node/sort-invocation.html

The commands sort -u and sort | uniq are equivalent, but this equivalence does not extend to arbitrary sort options. For example, sort -n -u inspects only the value of the initial numeric string when checking for uniqueness, whereas sort -n | uniq inspects the entire line.

As your numeric string is 1, you only get the first line.

0

Instead of using above mentioned sort command We can try with awk command

awk '{if (!seen[$0]++)print }' filename

output

1e971713-31e4-62d0-a3d1-37c0610c10e5
1e97d59f-bc32-6126-b7b1-0b52ddd35273

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.