Looking for valid IP Addresses

IP Addresses have the format 3DIGITS.3DIGITS.3DIGITS.3DIGITS

So, the expression for find these values is quite simple:

$ grep -P "^([\d]{1,3}\.){3}[\d]{1,3}$" file.txt
127.0.0.1
129.22.224.55
192.168.1.1
123.255.255.255
Where:
^ : at the beginning
[\d]{1,3} : find 1, 2 or 3 digits
\. : those numbers must be followed by a .
{3} : we are looking for 3 groups with the same pattern
[\d]{1,3} : find a block of 1,2 or 3 digits
$ : at the end

For this example we have used a file with this content:

127.0.0.1
129.22.224.55
192.168.1.1
123.12.12
11.00.00.1.2
123.255.255.255
12.12.12.12.12

If you like this post please pay me with a click on the ads :)