sort(1) sorting IP (v4) addresses (INET_ATON?)

Chris H bsd-lists at BSDforge.com
Thu Nov 30 21:53:26 UTC 2017


On Thu, 30 Nov 2017 19:15:23 +0800 "Julian Elischer" <julian at freebsd.org> said

> On 30/11/17 3:09 am, Rodney W. Grimes wrote:
> >> I'm constantly dealing with IPv4 addresses (millions). Sorting the mass
> >> is never perfect, and I'm forced to *visually* fix those out of order.
> >> Yet I continue to (later) find some I've missed -- I'm sick of it!
> >> Thus far, I've found I attain the best results with sort(1):
> >>
> >> sort -t . -k 4,4n -k 3,3n -k 2,2n  ./TCPLIST | sort -g>./SORTED
> >>
> >> Which ends up pretty damn close. But not perfect. :(
> >> Then it occurred to me that adding INET_ATON to sort, allowing for
> >> an additional -inet option to sort(1) for IP addresses -- hell if
> >> MySQL can provide it[1], why the heck can't sort(1)?
> >> Else I guess I'll have to try and give a shot at cobbling up a C
> >> utility to manage it. But seems like too much work for something
> >> that'll only accomplish one thing. :(
> >>
> >> Thanks for any thoughts, or further insight.
> >>
> >> [1]
> > https://dev.mysql.com/doc/refman/5.7/en/miscellaneous-functions.html#function_inet-aton
> > The old school on this is you do a substitue of the . for a <sp>
> > and then just use sort -n on that then munge it back to have the .'s,
> >
> > Though I would not be opposed to sort learning about inet numbers,
> > it should not be -inet as you would want to be able to apply
> > this to fields as in 4i to sort the 4th field as an inet number.
> > Actually I guess you need both to be orthagnal with -n and others.
> >
> >
> and now to try make it work for compacted IPv6 numbers...
I thought about that also, while I was attempting to sort (oh pun!) this
all out, and came up with the idea of packing the IP's up as HEX hashes.
They actually were easier to sort. For example; the IP for ns1.elischer.org,
and vps1.elischer.org become: CC6D3F10, or cc6d3f10, depending on which you
prefer. It also makes the size (length) smaller. In this case, we went to
what would have been 13 characters to only 8. :)
It was pretty simple; just sh(1), tr(1), dc(1), and the tiniest bit of math. :)

--Chris




More information about the freebsd-hackers mailing list