Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 30 Nov 2017 13:54:00 -0800
From:      "Chris H" <bsd-lists@BSDforge.com>
To:        "Julian Elischer" <julian@freebsd.org>
Cc:        "FreeBSD Hackers" <freebsd-hackers@freebsd.org>, "Rodney W. Grimes" <freebsd-rwg@pdx.rh.CN85.dnsmgr.net>
Subject:   Re: sort(1) sorting IP (v4) addresses (INET_ATON?)
Message-ID:  <59da92627fcc7edec9bb7708156b8bcc@udns.ultimatedns.net>
In-Reply-To: <cb943aeb-f600-3b70-71f4-bd2579e03c35@freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 30 Nov 2017 19:15:23 +0800 "Julian Elischer" <julian@freebsd=2Eorg> s=
aid

> On 30/11/17 3:09 am, Rodney W=2E Grimes wrote:
> >> I'm constantly dealing with IPv4 addresses (millions)=2E Sorting the mas=
s
> >> is never perfect, and I'm forced to *visually* fix those out of order=2E
> >> 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 =2E -k 4,4n -k 3,3n -k 2,2n  =2E/TCPLIST | sort -g>=2E/SORTED
> >>
> >> Which ends up pretty damn close=2E But not perfect=2E :(
> >> 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=2E But seems like too much work for something
> >> that'll only accomplish one thing=2E :(
> >>
> >> Thanks for any thoughts, or further insight=2E
> >>
> >> [1]
> > https://dev=2Emysql=2Ecom/doc/refman/5=2E7/en/miscellaneous-functions=2Ehtml#fu=
nction_inet-aton
> > The old school on this is you do a substitue of the =2E for a <sp>
> > and then just use sort -n on that then munge it back to have the =2E'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=2E
> > Actually I guess you need both to be orthagnal with -n and others=2E
> >
> >
> and now to try make it work for compacted IPv6 numbers=2E=2E=2E
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=2E
They actually were easier to sort=2E For example; the IP for ns1=2Eelischer=2Eorg=
,
and vps1=2Eelischer=2Eorg become: CC6D3F10, or cc6d3f10, depending on which you
prefer=2E It also makes the size (length) smaller=2E In this case, we went to
what would have been 13 characters to only 8=2E :)
It was pretty simple; just sh(1), tr(1), dc(1), and the tiniest bit of math=
=2E :)

--Chris





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?59da92627fcc7edec9bb7708156b8bcc>