Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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: 


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.

share|improve this question
It will match 999.999.999.999 too. –  cYrus Oct 24 '10 at 12:11
So, you only want to grep IPv4 addresses, right? –  Arjan Oct 24 '10 at 12:51
And as for you example: would you expect or or no match at all? –  Arjan Oct 24 '10 at 12:54
about and they valid IP I accept –  jennifer Oct 24 '10 at 12:56
Technically, IP addresses such as 192.1.4097 are valid and accepted by Linux glibc and Windows. –  grawity Oct 24 '10 at 14:49

8 Answers 8

up vote 24 down vote accepted

try this:

grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /etc/hosts

which matches all expressions from to 999.999.999.999


grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /etc/hosts

you will get IP addresses only

on solaris probably egrep will do the job.

share|improve this answer
I try the grep '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' /etc/hosts but I dont get anything -:( –  jennifer Oct 24 '10 at 13:04
@jennifer, you'll need to enable extended regular expressions: grep -E <pattern> <file> (or, to just print the matches: grep -Eo <pattern> <file> –  Arjan Oct 24 '10 at 13:08
like this ? grep -E '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' /etc/hosts –  jennifer Oct 24 '10 at 13:14
@udo: Well this matches but it hides the last .1 from the output, I can't see how it can help. –  cYrus Oct 25 '10 at 16:21
Your regexp doesn't match –  Stefan Seidel Sep 13 '12 at 8:36

How's this:

perl -MRegexp::Common=net -ne '/($RE{net}{IPv4})/ and print "$1\n"' /etc/hosts
share|improve this answer
Nice! (This also returns as, which I think is fine for the question asker.) –  Arjan Oct 24 '10 at 13:57
If conformant IP addresses are really wanted, this is the only type of solution that has any chance of being close to complete. When I saw the question, I just thought "I won't touch that with a ten-foot standard regexp pole". Caveats, caveats everywhere :-) . –  Daniel Andersson Sep 13 '12 at 11:03


-w / --word-regexp 

flag to grep makes it only match on word boundaries, meaning that your match must either be surrounded by whitespace or begin / end at the beginning / end of the line!

share|improve this answer
if [ ` echo $ip | '^((25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[1-9][0-9]?)$'  | grep -o "\." | wc -l` -eq 1 ];
then ipv4=true;
share|improve this answer

A little tricky, but it should work:

( X='\([0-9]\{1,2\}\|1[0-9]\{2\}\|2[0-4][0-9]\|25[0-5]\)' ; grep "\([^\.]\|^\)$X\.$X\.$X\.$X\([^\.]\|$\)" file )
share|improve this answer
What about then? ;-) –  Arjan Oct 24 '10 at 15:20
As far as I know IPs doesn't have padding zeros. –  cYrus Oct 24 '10 at 15:22
Hmmm, ping surely works on my Mac. But then: I just learned that even ping 2130706433 yields the very same result. :-) Oops, ping 00127.00000.00000.00001 translates to Odd... Or octal maybe? Yes, octal for sure, so you're right about leading zeroes I guess. –  Arjan Oct 24 '10 at 15:23
Yes, 00127 (octal) = 87 (decimal). Surely they're all valid IPs, but I guess that's not the standard way to represent them. Anyway that's not requested by the asker. –  cYrus Oct 24 '10 at 15:33

To only find matches with 4 octets exactly (excluding things like use this:

grep -P '(?<=[^0-9.]|^)[1-9][0-9]{0,2}(\.([0-9]{0,3})){3}(?=[^0-9.]|$)'

It should never detect non-IP-addresses. The expression could be more complex to verify more things but this should work for most cases. It will not match a preceding 0 since is not a common way to write IP addresses.

share|improve this answer

grep -E '^((25[0-5]|2[0-4][0-9]|[1]?[1-9][0-9]?).){3}(25[0-5]|2[0-4][0-9]|[1]?[1-9]?[0-9])$'

Modified version of Arnaud B.'s answer.

This expression will not match IP addresses with leading 0s. e.g., it won't match This expression will not match IP addresses with more than 4 octets. e.g., it won't match

share|improve this answer

Regular expression for matching an ip address in TCL

set a ""

if {[regexp {^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$} $a]} {

puts "yes" }

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.