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

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

the problem: its match also words(IP) that have more then 4 octet :

1.1.1.1.1 

or 192.1.1.1.160

my question

how to match VALID IP and only IP address with only 4 octets

remark: I can accept also perl - one line syntax solution (if grep dont help us)

link|improve this question

79% accept rate
3  
It will match 999.999.999.999 too. – cYrus Oct 24 '10 at 12:11
2  
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
2  
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
feedback

4 Answers

up vote 2 down vote accepted

try this:

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

which matches all expressions from 1.0.0.0 to 999.999.999.999

with

grep -Eo '[1-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.

link|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
I updated my answer -> this should work now – udo Oct 24 '10 at 13:33
the problem is that -E isnt valid flag in solaris? -:( – jennifer Oct 24 '10 at 13:39
show 4 more comments
feedback

How's this:

perl -MRegexp::Common=net -ne '/($RE{net}{IPv4})/ and print "$1\n"' /etc/hosts
link|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
feedback

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 )
link|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
feedback

Well, this depends on whats the file like, maybe you can add an end of line anchor or a whitespace class. But overall this don't seem to be an easy question.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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