bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Eric Blake
Subject: bug#23556: sort(1): misleading description of option -n
Date: Mon, 16 May 2016 13:35:48 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0

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

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]