2. Sort
3. Convert back
python3 -c 'import ipaddress, sys; print("\n".join(sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
It takes the IP addresses to be sorted on the command line.Or, re-abbreviating them by removing zeroes and attempting to use :: where possible:
python3 -c 'import ipaddress, sys; print("\n".join(str(ipaddress.IPv6Address(y)) for y in sorted(ipaddress.IPv6Address(x).exploded for x in sys.argv[1:])))'
Both of these versions will crash if given input that isn't syntactically valid as an IPv6 address. 1) convert to a non-space-zeros-compressing hex string
2) sort on the hex string
3) convert back through inet_ntop()
Only a minor variant needed to deal with prefix/len sort order.This post also highlights a major thing I discovered when deploying and using IPv6, which is that you don't "Lift and shift" IPv4 to IPv6.
This is one of the reason its hard to deploy, because people cannot use the same IPv4 concepts to IPv6. For unknown reason they do, they will find the same problem they had with v4.
sargstuff•7h ago
1) Regular expression / seamingly lost art of sprintf formatting are some methods to normalize 001:db8::2:1 to something usable for sorting aka 2001:db8:0000:0000:0000:0000:0002:0001. Perhaps restoring to rfc 5952 format when printing sorted results.
2) Modify hex to 'sortable utf-16 characters', modify back post sort[1]
3) avoid utf-8 / utf-16 issues, use relevant python libraries to handle ipv4/ipv6[2][3],[4]: ip2n < file | sort -n | n2ip
----------------------------
[0] : https://datatracker.ietf.org/doc/html/rfc5952
[1] : https://stackoverflow.com/questions/5797369/unix-sort-utilit...
[2] : https://stackoverflow.com/questions/75522231/how-to-sort-ipv...
[3] : https://www.geeksforgeeks.org/convert-ip-address-to-integer-...
[4] : https://ipfind.com/blog/how-to-convert-ip-address-to-integer...