Subject: bug#23951: sort -k1.4,1.6n -u HAVE BUGS!!!

From: Assaf Gordon <[email-masked]>
Date: Wed, 13 Jul 2016 20:38:52 -0400

tag 23951 notabug
close 23951
stop

Hello,

quoting an off-list email from David:
> When I use "sort -k1.4n -u", system output miss [c3m1.ecld.com].
> then "sort -k1.4n aa |sort -u" the [c3m1.ecld.com] appear again.

This is not a bug but correct behavior.

The parameter "-k1.4n" means that the compared keys start at the digit (e.g. "1", "2") -
thus the compared key of both "c3a1" and "c3m1" is "1", and "-u" outputs only one of them.
using "sort -u" separately treats the entire line as the key, thus "c3a1" and "c3m1" are different.

The following will demonstrate:

  $ printf "aaa1\nbbb2\nccc1\n" | sort -k1.4n
  aaa1
  ccc1
  bbb2

  $ printf "aaa1\nbbb2\nccc1\n" | sort -k1.4n -u
  aaa1
  bbb2

  $ printf "aaa1\nbbb2\nccc1\n" | sort -k1.4n | sort -u
  aaa1
  bbb2
  ccc1

In the first two examples, the letters do not matter at all. Because the key is "-k1.4",
the first three characters are ignored, and the compared values are the digits 1,2,1.
In the second example, asking for unique values refer to unique keys, meaning lines with key "1"
will be printed once (aaa1).

In the third example, the additional 'sort' negates the first 'sort', because it first sorts all lines alphabetically, then prints unique lines, while using the entire line as a key.

Recent versions of 'sort' support the '--debug' option, which helps troubleshooting such cases:
===
$ printf "aaa1\nbbb2\nccc1\n" | sort --debug -k1.4n 
sort: using ‘en_US.UTF-8’ sorting rules
sort: leading blanks are significant in key 1; consider also specifying 'b'
sort: key 1 is numeric and spans multiple fields
aaa1
   _
____
ccc1
   _
____
bbb2
   _
____

===

As such, I'm closing this bug, but discussion can continue by replying to this thread.

regards,
 - assaf