[conspire] Puzzle: How do you sort IP address lists?

Nick Moffitt nick at zork.net
Tue Nov 7 12:37:29 PST 2006


Rick Moen:
> Personally, I played with these things for a while, gave up and
> switched to awk, and had the problem mostly solved with a rather
> ghastly script when I thought "Wait a second!  That's absurd.  We
> _should_ be able to do this using just GNU sort.  If it can't sort IP
> addresses, what the hell good is it?"
> 
> So, I went back and eventually figured it out -- and I'm wondering if
> any other subscriber has either already solved this problem or cares
> to take a crack at it.

I have run into this in the past, actually, only with timestamps.  I
ended up doing a first pass sort, then breaking it up and sorting within
using -t and -k to set the field separator and sort starting field,
respectively.  I ended up doing three successive runs, in reverse order,
and naming the files with the prefixes.  I then did a final sort to get
the filenames in the order I wanted and catted them together.  It was a
one or two-liner that has since expired from my bash_history, but I was
cursing and spitting the whole time.

But as I look now, it seems that you can specify multiple -k entries,
and force it to sort on the column alone by specifying an end to the
sort criterion as well:

	sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

I'm kind of alarmed that GNU sort hasn't picked up more sort contexts.
I found myself in dire need of a hex sort a while ago, and ended up
resorting to python.  

On a vaguely related tangent, Ryan Finnie has packaged cidrgrep in sid,
and it should be in testing by now.  Hooray for grep using CIDR ranges
instead of regexes!  Why doesn't grep have this already?  It makes me
wonder if there are more pattern specifications we use that would be
useful as options to common tools.

-- 
"N'aimez pas votre voiture?                             Nick Moffitt
Alor, l'heure est arrive pour la brulé!"          nick at teh.entar.net
	-- Mark Jaroski




More information about the conspire mailing list