Re: Feature Request for 'sort' utility (original) (raw)


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


From: Pádraig Brady
Subject: Re: Feature Request for 'sort' utility
Date: Mon, 27 Jun 2011 15:34:01 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100227 Thunderbird/3.0.3

On 27/06/11 13:53, Eric Blake wrote:

On 06/26/2011 06:05 PM, Bob Proulx wrote: > scott n-h wrote: >> -I or --ip-address >> I'm hoping the usefulness of this would be apparent. > > The coreutils manual: > > > http://www.gnu.org/software/coreutils/manual/htmlnode/sort-invocation.html#sort-invocation > > Shows an example of sorting IPv4 addresses this way: > > sort -s -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n

However, there's no easy way to sort IPv6 addresses using existing sort keys, so maybe there is indeed room for improvement, if someone would care to contribute code.

Well the argument here is whether IP addresses are a fundamental enough type to support directly within sort, or whether using the Decorate Sort Undecorate pattern is more appropriate.

Here is an example of using DSU with funcpy¹ which handles the various formats IPV6 addresses can be in:

printf '%s\n' ::03 ::2 ::1 | funcpy --import socket,struct " ':'.join('%04x' % x for x in struct.unpack('>HHHHHHHH', socket.inet_pton(socket.AF_INET6, x.split()[0])))+'\t'+x " | sort | cut -f2-

While being more general, this is fairly awkward. Also there is a binary to ascii step that would not be required if done internally. I guess if we were supporting this we'd use the 'I' option. I'm 50:50 on adding this though, as I'm not sure how common it is to need to sort IP addresses?

cheers, Pádraig.

¹ http://www.pixelbeat.org/scripts/funcpy