Reese Knowledgebase

Sort IP Addresses in bash

View Kristian Reese's profile on LinkedIn


If you like this article, please +1 or Recommend via FB with the provided buttons above:

Article ID: 92
by: Reese K.
Posted: 20 Feb, 2013
Last updated: 20 Feb, 2013
Views: 2639

Sort list of IP addresses via command line

Do you have a text file filled with a bunch of IP addresses, and it won't sort properly with the -n option alone?  Providing additional options can help achive the desired output.

$ cat nfs_clients | sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4

A description of the additional options from the sort man page:

       -k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default end of line)

       -t, --field-separator=SEP
              use SEP instead of non-blank to blank transition

From the command line example above, we've indicated the 'dot' or 'period' to be the field-separator and have defined keys in each of the IPs octets.

This article was:   Helpful | Not Helpful
Also listed in
folder UNIX
folder UNIX -> Solaris
folder UNIX -> Scripting

Prev   Next
EXT3-fs warning Directory index full!     Installing Teradata Client 13.10 on RedHat 6 x86_64

RSS