Sort IP address – Bash

Sometimes you wonder how to sort IP address from a large pool of IP address consisting different subnets.

Here is a simple command which can do the task for you

Sample input file

192.168.1.100
192.168.1.2
10.10.1.100
10.10.2.20
192.168.1.3

sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /path to file

Output

10.10.1.100
10.10.2.20
192.168.1.2
192.168.1.3
192.168.1.100

-t . : Used as field seperator, here (dot) is field seperator
-n : Sorts in numerical value
-k : Sorts the data field using the specific column number, here the sorting starts from first field, then second, and so on.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts