bug-gnu-utils
[Top][All Lists]
Advanced

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

Re: sort -nu: bug or feature?


From: Chris F.A. Johnson
Subject: Re: sort -nu: bug or feature?
Date: Wed, 8 Sep 2004 11:39:19 -0400 (EDT)

On Wed, 8 Sep 2004, Andreas Schwab wrote:

Andrew Noymer <address@hidden> writes:

sort -nu seems to compare only the first key, which is not what a lot of
people expect.  I don't know if this is a bug, though.

sort -n takes the selected fields (the whole line if no -k is used) and
tries to parse a number out of each starting from the beginning of the
field, ignoring leading whitespace and discarding trailing garbage.  The
resulting numbers are the sort keys, which also decide which lines are
equivalent for -u.

But I think most folks expect that "sort -nu" == "sort -n | uniq"

The expectation is wrong because sort -n and uniq use different sort keys..
Especially uniq is always using a string comparison.  Compare this:

$ printf "%s\n" 1 01 | sort -nu
1
$ printf "%s\n" 1 01 | sort -n | uniq
01
1

    "sort -n" uses a string comparison after aligning the numbers on
    the decimal point, but -u apparently uses a (non-POSIX) numeric
    comparison to determine equivalency:

$ printf "%s\n" 1 01 08 8 | sort -nu
1
08


    Also, there's a bug in the documentation; at the end of the man
    page:

-------------
       The full documentation for sort is maintained as a Texinfo
       manual.  If the info and coreutils sort programs are properly
       installed at your site, the command

              info coreutils sort

       should give you access to the complete manual.
------------------

    This brings up the page on sorting the output of ls; to get the
    info page for sort, use "info sort".


--
        Chris F.A. Johnson                      http://cfaj.freeshell.org
        =================================================================
                Everything in moderation -- including moderation




reply via email to

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