Take the 2-minute tour ×
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:

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.

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

7 Answers

up vote 14 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 0.0.0.0 to 999.999.999.999

with

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

note:
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
2  
@udo: Well this matches 1.1.1.1.1 but it hides the last .1 from the output, I can't see how it can help. –  cYrus Oct 25 '10 at 16:21
1  
Your regexp doesn't match 10.0.0.1 –  Stefan Seidel Sep 13 '12 at 8:36
show 6 more comments

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 192.1.1.1.160 as 192.1.1.1, which I think is fine for the question asker.) –  Arjan Oct 24 '10 at 13:57
2  
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
add comment

The

-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
add comment
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;
else 
ipv4=false;
share|improve this answer
add comment

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 127.000.000.001 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
1  
Hmmm, ping 127.000.000.001 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 87.0.0.1. 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
add comment

To only find matches with 4 octets exactly (excluding things like 1.1.1.1.1) 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 010.1.12.1 is not a common way to write IP addresses.

share|improve this answer
add comment

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 192.168.1.01 This expression will not match IP addresses with more than 4 octets. e.g., it won't match 192.168.1.2.3

share|improve this answer
add comment

Your Answer

 
discard

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.