Jan 23, 2014

Sort IP Address

There is time you need to sort IP address in the way human understand.
# sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 addresses.txt

Where,
  • -t . : Set field to . (dot) as our IPs separated by dot symbol
  • -n : Makes the program sort according to numerical value
  • -k opts: Sort data / fields using the given column number. For example, the option -k 2 made the program sort using the second column of data. The option -k 3,3n -k 4,4n sorts each column. First it will sort 3rd column and then 4th column.