9
Dec/08
0

Quickie: Sort IP-Addresses on linux command line (bash)

I have some ip-addresses in a text file. One address per line. The addresses are unsorted. I’d like to have them in a correct order. Some example from file “addresses”:

192.168.1.1
192.168.1.4
80.68.11.31
192.168.10.33
192.168.11.3
81.67.12.31

Now I run sort with the following options to sort these ip addresses:

:> sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n addresses

The sort command sends this to stdout:

80.68.11.31
81.67.12.31
192.168.1.1
192.168.1.4
192.168.10.33
192.168.11.3

Short explanation for the params:

-t .: Use dot as field seperator.
-k 1,1n: First sort key is field 1, sort it numeric.

The lines are sorted by each sort key one after another.

Comments (0) Trackbacks (0)

No comments yet.

No trackbacks yet.