--- Begin Message ---
Subject: |
[PATCH] Fix handling of non-numeric keys with '-n' |
Date: |
Wed, 20 Jul 2011 05:01:08 -0400 (EDT) |
There's a possible BUG when using '-n' that any non-numeric key is handled
as equal to '0'. This results in unexpected behavior eg. when using '-nu'.
$ for i in NUMBERS 10 9 0; do echo $i; done| sort -n
NUMBERS
0
9
10
$ for i in NUMBERS 10 9 0; do echo $i; done| sort -nu
NUMBERS
9
10
See the '0' is mysteriously gone. I believe the semantics should be changed,
either by honoring the string representation of non-numeric key (see patch),
alternatively by ignoring all lines with non-numeric keys.
---
src/sort.c | 11 +++++++++++
1 files changed, 11 insertions(+), 0 deletions(-)
diff --git a/src/sort.c b/src/sort.c
index 3d3119d..58f96e6 100644
--- a/src/sort.c
+++ b/src/sort.c
@@ -1907,6 +1907,17 @@ numcompare (char const *a, char const *b)
while (blanks[to_uchar (*b)])
b++;
+ /* Put non-numeric keys before all numbers.
+ If both non-numeric, don't return 0 unless equal. */
+ if (!ISDIGIT(*a) && *a != '-') {
+ if (!ISDIGIT(*b) && *b != '-')
+ return strcmp(a, b);
+ return -1;
+ }
+ else if (!ISDIGIT(*b) && *b != '-') {
+ return 1;
+ }
+
return strnumcmp (a, b, decimal_point, thousands_sep);
}
--
1.7.4.4
--- End Message ---
--- Begin Message ---
Subject: |
Re: bug#9128: [PATCH] Fix handling of non-numeric keys with '-n' |
Date: |
Wed, 20 Jul 2011 19:38:59 -0600 |
User-agent: |
Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.1.11-1.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.11 |
tag 9128 notabug
thanks
On 07/20/2011 03:01 AM, Zdenek Pavlas wrote:
There's a possible BUG when using '-n' that any non-numeric key is handled
as equal to '0'. This results in unexpected behavior eg. when using '-nu'.
Thanks for the report. However, this behavior is required by POSIX:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html
-n
Restrict the sort key to an initial numeric string, consisting of
optional <blank> characters, optional minus-sign, and zero or more
digits with an optional radix character and thousands separators (as
defined in the current locale), which shall be sorted by arithmetic
value. An empty digit string shall be treated as zero. Leading zeros and
signs on zeros shall not affect ordering.
On the line "NUMBERS", the initial numeric string is empty, hence the
line is sorted with the value of 0.
+++ b/src/sort.c
@@ -1907,6 +1907,17 @@ numcompare (char const *a, char const *b)
while (blanks[to_uchar (*b)])
b++;
+ /* Put non-numeric keys before all numbers.
+ If both non-numeric, don't return 0 unless equal. */
+ if (!ISDIGIT(*a)&& *a != '-') {
Sorry, we can't take this patch, as it would violate POSIX and probably
break existing scripts that expect sort to treat non-numeric lines as
sorting identically with 0.
--
Eric Blake address@hidden +1-801-349-2682
Libvirt virtualization library http://libvirt.org
--- End Message ---