?

Log in

No account? Create an account
Apparently there's a bug in sort ... or something. - He's just this guy, you know.

> Recent Entries
> Archive
> Friends
> Profile

Links
Schlock Mercenary
Something Positive
Megatokyo
Irregular Webcomic
Sluggy Freelance

December 21st, 2007


Previous Entry Share Next Entry
03:30 pm - Apparently there's a bug in sort ... or something.
So, a strange Unix thing I noticed today.

Given the following file :

> cat list2
1
1
2
A
B


Now try sorting it. First with just "-u" (sort uniquely) :

> sort -u list2
1
2
A
B


Now add "-n" to sort numerically (if the field looks like a number) :

> sort -un list2
A
1
2


Where did the "B" go ??
I've seen this in sort under Solaris 8, the GNU version in Ubuntu 7.04 and the gnuwin32 version on Windows. Surely it's a bug ?
Current Mood: geekygeeky

(4 touches | En garde !)

Comments:


[User Picture]
From:drplokta
Date:December 21st, 2007 03:47 pm (UTC)
(Link)
A and B have the same numeric value (i.e. not a number), so when you sort by unique numeric values you only see one of them.
[User Picture]
From:the_magician
Date:December 21st, 2007 04:14 pm (UTC)
(Link)
What he said!

"-n, --numeric
An initial numeric string, consisting of optional white space, optional sign, and a nonempty string of digits with optional decimal point, is sorted by value."

as A and B consist of a line with zero digits etc. they are the same "no numeric value".

Thanks Mike, that would have puzzled me for a while too, as I'd have assumed it would have dropped both the A and the B rather than just putting one up. Or evaluated them as Hex digits .... 0xA 0xB (which would have been silly) or by their Ascii value (even sillier!)
[User Picture]
From:davidcook
Date:December 21st, 2007 04:57 pm (UTC)
(Link)
Ah, that makes perfect logical sense. It's just slightly counter-intuitive at first glance :-)

It means "sort -un list1" produces different output to "sort -u list1 | sort -n". Hmmm ... wonder if any of my old scripts need to be revised to take this into account ?

[User Picture]
From:morniendil
Date:December 22nd, 2007 12:07 am (UTC)
(Link)
Well that's good to be aware of :)

> Go to Top
LiveJournal.com