bug-coreutils
[Top][All Lists]
Advanced

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

Re: sorting on space gives unexpected behaviour with sort 6.10


From: Eric Blake
Subject: Re: sorting on space gives unexpected behaviour with sort 6.10
Date: Wed, 04 Jun 2008 06:50:47 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080421 Thunderbird/2.0.0.14 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Dirk-Jan Faber on 6/4/2008 6:29 AM:
| Hello,
|
| When trying to sort input that has spaces in it I am getting unexpected
behaviour. When using the inputfile:

Not a bug.  Your problem is that you aren't specifying the correct keys.

|
| Trying to sort this input gives:
| $ cat in.txt | sort -t' ' -n -k1n -k2n -k3n

Useless use of cat.

|
| I would have expected the output to be different, the "9 5 10" line
ought to be before the line starting with the ten.

You are requesting the first key start at the first field, treated
numerically, and extending through the end of the line.  What you wanted
was to specify the first key as numeric, but ending at the first field.
Also, since you specified -n globally, you don't need to repeat it for the
keys:

sort -t' ' -n -k1,1 -k2,2 -k3,3 < in.txt

| This bug can be bypassed by replacing the spaces with another character
like the semicolon:
| $ cat in.txt | tr ' ' ':'  | sort -t':' -n -k1n -k2n -k3n | tr ':' ' '

Another useless use of cat (hint - "tr ' ' ':' < in.txt" is more efficient
than "cat in.txt | tr ' ' ':'").  The reason this worked around your
problem is that ':' is greater than '9', but ' ' is less than '0', when
the rest of the line is tacked on to the end of your sort key.

| $ sort --version
| sort (GNU coreutils) 6.10

Consider upgrading - the latest stable version is 6.12.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkhGj6cACgkQ84KuGfSFAYB5qgCfQ5DmGRpGq1vRFknuzLJs1E5b
KjIAnR9ga82tvk5ySFfzvqTnKGpsFMj2
=Jeow
-----END PGP SIGNATURE-----




reply via email to

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