Sorting IPv4 Addresses with GNU Sort

While processing some rather large lists of addresses as part of a side project, I needed to be able to sort them in a numerical order within a shell script. I had a file with lines like:

  • 69.90.132.19
  • 69.90.132.22
  • 66.152.91.84
  • 208.122.204.181
  • 69.90.132.22
  • 69.90.132.31
  • 216.131.106.249
  • 216.131.84.26
  • 67.55.105.252
  • 208.64.44.102

Standard sort using sort –n only sorts on the first octet, and although it’s a improvement on alphabetic sorting its not ideal. The solution comes in specifying a pile of switches to sort:

sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

This gets it sorted in Numerical order, by octet, using a period (dot) as a separator between octets. Combining this with a –u flag gives one a nicely sorted, unique list of IP addresses. This could probably be extended to IPv6 without too much hastle.

Share this post: Share this post with the world.
  • TimesURL
  • Muti
  • Facebook
  • StumbleUpon

Tags: ,

Leave a Reply