Subject: bug#20745: I would like to make a request for the sort command

From: Stephane Chazelas <[email-masked]>
Date: Mon, 8 Jun 2015 10:51:59 +0100

2015-06-08 11:16:37 +0200, Erik Auerswald:
[...]
> FWIW I use 'sort' to sort IPv4 addresses in my ping_scan[1] script.
> 
> The info documentation for sort provides another example, log files
> sorted by IP address and time stamp. That specific example even needs
> two runs of sort, because sort lacks built-in support for IP addresses.
> 
> While IPv4 addresses are readily sorted by "sort -s -t '.' -k 1,1n -k
> 2,2n -k 3,3n -k 4,4n", this is not the case for IPv6 addresses. Having
> an option for sorting IP addresses that supports both IPv4 and IPv6
> seems like a useful addition to me.
[...]

In the spirit of tools doing one thing and doing it well, it
would make more sense to have a tool that converts an IP address
to something sortable and use that instead in combination with
sort.

I'm not even sure having a tool just for that specific task
would make sense though. Here, it sounds more like a job for a
high level language like perl/python... (what if I want to sort
on roman numerals now, week day names, astrological signs...)

for instance, here using yash syntax (you can use named pipes or
possibly coprocs with some other shells):

ip2hex() {
  perl -MSocket=:all -nle '
    print unpack "(H2)*", inet_pton(/:/?AF_INET6:AF_INET, $_)'
}

mysort() {
  (
    exec 3>>|4
    tee /dev/fd/3 |
      cut -f1 3>&- | ip2hex 3>&- |
      paste - /dev/fd/4 3>&-
  ) | sort | cut -f2-
}

mysort << EOF
127.0.0.1       blah
6.6.6.6         foo
::1             bar
EOF

That's still quite awkward. A shame that piping capabilities in
shells don't extend to  more  complex scenarii where the output
of some command can be piped to two others the output of which
can be merged back easily.

named pipes can be used for that, but cleaning up and
restricting access to them makes their usage quite messy.

Of course, the whole thing can be done with perl.

-- 
Stephane