What regular expression can I use to match an IP address?

Posted by jennifer on Super User See other posts from Super User or by jennifer
Published on 2010-10-24T12:00:49Z Indexed on 2012/09/13 9:41 UTC
Read the original article Hit count: 291

Filed under:
|
|

With the following grep syntax I want to match all IP address in a file (from a ksh script)

  grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' file

The problem: It also matches words (IP) that have more then 4 octets:

1.1.1.1.1 

or

192.1.1.1.160

How can I match a valid IP and only IP addresses with 4 octets? I can also use Perl – a one line syntax solution, if grep doesn't work.

© Super User or respective owner

Related posts about regex

Related posts about grep