GNU bug report logs - #23556
sort(1): misleading description of option -n

Previous Next

Package: coreutils;

Reported by: Carsten Hey <carsten <at> debian.org>

Date: Mon, 16 May 2016 18:29:01 UTC

Severity: normal

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Eric Blake <eblake <at> redhat.com>
To: Carsten Hey <carsten <at> debian.org>, 23556 <at> debbugs.gnu.org
Subject: bug#23556: sort(1): misleading description of option -n
Date: Mon, 16 May 2016 13:35:48 -0600
[Message part 1 (text/plain, inline)]
On 05/14/2016 08:17 AM, Carsten Hey wrote:
> Hi,
> 
> the man page sort(1) contains a misleading description of the option -n:

>     compare according to string numerical value

That sounds accurate to me, although as Assaf pointed out, suggested
wording improvements are welcome.

>     $ printf '%s\n' 'x 9' 'x 10' | sort -n
>     x 10
>     x 9

The numerical value of "x 10" (that is, the equivalent to the value of
atoi("x 10")), is "0".  To check:

$ printf '%s\n' 'x 9' 'x 10' '1' '-1' | sort -n
-1
x 10
x 9
1

If you want to sort by the second column only, then use:

$ printf '%s\n' 'x 9' 'x 10' | sort -n -k 2,2
x 9
x 10

That is, by adding the -k option, you can limit the text being sorted to
the portion of the line containing the numerical value, rather than the
entire line, so as to avoid a numerical value of 0 when hitting a
non-numeric portion of the line.

> …, but instead, -n stops doing its magic after finding the first
> non-numeric, non-whitespace character.

Because that is how it has always behaved, and how POSIX requires it to
behave.

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

This bug report was last modified 6 years and 211 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.