how to sort ipv4 addresses with unix sort

this will sort a list of ipv4 addresses:

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

the -g option in addition to all the n’s in the field definitions may seem like overkill, but i found it necessary on more than one occasion. this is sort of a paranoid command line 'cos i sometimes work with lists of addresses that number in the thousands, so i don’t want to have to validate the output manually; this has never failed to give the correct output.


sort -g -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n <<EOM