How to sort ip addresses

I don't know where I found this solution but it is very easy to remember.

$ cat ip_file
10.0.0.2
10.3.0.1
192.168.0.3
192.168.9.11
192.168.9.10
10.2.2.1
10.2.2.21
10.2.2.17
192.168.2.1
10.0.0.1
10.0.0.100
$ sort -t. +0n -1n +1n -2n +2n -3n +3n ip_file
10.0.0.1
10.0.0.2
10.0.0.100
10.2.2.1
10.2.2.17
10.2.2.21
10.3.0.1
192.168.0.3
192.168.2.1
192.168.9.10
192.168.9.11
Milosz Galazka's Picture

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.

Gdansk, Poland https://sleeplessbeastie.eu