The lack of a good command line way to sort IPv6 addresses

May 18, 2025

A few years ago, I wrote about how 'sort -V' can sort IPv4 addresses into their natural order for you. Even back then I was smart enough to put in that 'IPv4' qualification and note that this didn't work with IPv6 addresses, and said that I didn't know of any way to handle IPv6 addresses with existing command line tools. As far as I know, that remains the case today, although you can probably build a Perl, Python, or other language program that does such sorting for you if you need to do this regularly.

Unix tools like 'sort' are pretty flexible, so you might innocently wonder why it can't be coerced into sorting IPv6 addresses. The first problem is that IPv6 addresses are written in hex without leading 0s, not decimal. Conventional sort will correctly sort hex numbers if all of the numbers are the same length, but IPv6 addresses are written in hex groups that conventionally drop leading zeros, so you will have 'ff' instead of '00ff' in common output (or '0' instead of '0000'). The second and bigger problem is the IPv6 '::' notation, which stands for the longest run of all-zero fields, ie some number of '0000' fields.

(I'm ignoring IPv6 scopes and zones for this, let's assume we have public IPv6 addresses.)

If IPv6 addresses were written out in full, with leading 0s on fields and all their 0000 fields, you could handle them as a simple conventional sort (you wouldn't even need to tell sort that the field separator was ':'). Unfortunately they almost never are, so you need to either transform them to that form, print them out, sort the output, and perhaps transform them back, or read them into a program as 128-bit numbers, sort the numbers, and print them back out as IPv6 addresses. Ideally your language of choice for this has a way to sort a collection of IPv6 addresses.

The very determined can probably do this with awk with enough work (people have done amazing things in awk). But my feeling is that doing this in conventional Unix command line tools is a Turing tarpit; you might as well use a language where there's a type of IPv6 addresses that exposes the functionality that you need.

(And because IPv6 addresses are so complex, I suspect that GNU Sort will never support them directly. If you need GNU Sort to deal with them, the best option is a program that turns them into their full form.)

PS: People have probably written programs to sort IPv6 addresses, but with the state of the Internet today, the challenge is finding them.


Comments on this page:

From 193.219.181.219 at 2025-05-19 05:08:00:

Python would indeed be my choice if that ever becomes necessary.

print(*sorted(map(ipaddress.ip_address, (x.strip() for x in sys.stdin))), sep="\n")

I think the output of the `aggregate6` tool (again Python) is sorted as a side effect, too.

This was discussed for GNU sort 14 years ago: https://lists.gnu.org/archive/html/coreutils/2011-06/msg00082.html There's a DSU solution mentioned there that leverages python.

GNU decorate, part of the GNU datamash project, can sort IPv6 addresses. It uses the decorate, sort, undecorate approach with an existing sort program.

$ printf -- '%s\n' ff02::1 2001:db8:1::1 fe80::33 2001:db8::15 | decorate -k1:ipv6
2001:db8::15
2001:db8:1::1
fe80::33
ff02::1

I had this exact problem, but very specifically needed to solve it in Google Sheets. In case anyone else has the same problem, steal/crib from me at https://github.com/davidgroves/ip-to-decimal-gsheets/

It was as basic as making a javascript function to convert the IPv6 address into a number, and then sorting on that number.

This was a fun post to stumble on. I wrote about using Factor from the command line to do it:

https://re.factorcode.org/2025/05/sorting-ipv6.html

The solution becomes:

 $ cat ips.txt | ./factor -e="read-lines [ parse-ipv6 ] sort-by write-lines"
Written on 18 May 2025.
« It's not obvious how to verify TLS client certificates issued for domains
Python, type hints, and feeling like they create a different language »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Sun May 18 22:54:24 2025
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.