Sorting ip Addresses in Numerical Order Bash

Here’s how you could order ip address in numerical order, sorting by each octect individually. Let’s say you have a file containing the following ips.

jorge@laptopNix$ cat address 
193.149.184.1
139.86.17.116
222.114.231.43
168.4.17.209
168.48.228.137
228.55.100.232
17.146.224.201
62.53.41.187
233.156.115.190
39.170.227.217

to sort them….

jorge@laptopNix$ sort address -n -k1,1 -k2,2 -k3,3 -k4,4
17.146.224.201
39.170.227.217
62.53.41.187
139.86.17.116
168.4.17.209
168.48.228.137
193.149.184.1
222.114.231.43
228.55.100.232
233.156.115.190

where -n tells to do a numeric sort and -k to start sorting at first field and end at first. Just repeat the process for every octect. Of course if you have a file containing class C only address you only would have to use -k4,4

Share This!

Add a Comment

Your email address will not be published.

Sign up for our Newsletter

Download "Google Search Pro"

Enter your email and stay on top of things,

Subscribe!