Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Technically, IP addresses such as 192.1.4097 are valid and accepted by Linux glibc and Windows. – grawity Oct 24 '10 at 14:49
Ah, I never knew! ping 2130706433, on OS X: PING 2130706433 ( 56 data bytes. – Arjan Oct 24 '10 at 15:15
@Arjan: 0x7f.1 and 0177.1 – grawity Nov 7 '10 at 10:23

10 Answers 10

up vote 29 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

I'm using egrep "^([0-9]{1,3}\.){3}[0-9]{1,3}" /etc/hosts to match IP adresses at the beginning of a line. It can also be used without the ^ to allow white spaces or other chars before the IP address.

[0-9]{1,3} --> this matches a number between 1 and 999.
\. --> this is to add the dot.
([0-9]{1,3}\.){3} --> get a number with a dot 3 times.
[0-9]{1,3} --> finally add the fourth number.
share|improve this answer

A shorter version of the long regex:

egrep '([1-2]?[0-9]{0,2}\.){3,3}[1-2]?[0-9]{0,2}' 

Please use grep -E or egrep as suitable to your OS version

share|improve this answer
welcome at superuser. This question already has several good answers. To help people understanding differences between them, please edit your answer and explain what makes it better / different from the other ones. – Máté Juhász Nov 17 '15 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .