Extract list of IP addresses from a text file.

I have an xml file with IP addresses all over the show. I want to print only the IP addresses and cut off any text before or after the IP address.

Example:
Note: The IP addresses (x.x.x.x) do not consistently appear in the xml file as per the pattern below. Sometimes there are text before and/or after, sometimes there are two, three IP addresses per line.

 Computers   
  blah939  
    IP Address x.x.x.x 
 
  blah3938 
    IP Address x.x.x.x 
 
  blah3938
    IP Address x.x.x.x 
 
  blah9383
    IP Address x.x.x.x  
 
  blah3983 
    IP Address x.x.x.x  

Output

x.x.x.x
x.x.x.x 
x.x.x.x 
x.x.x.x 
x.x.x.x 
x.x.x.x 
x.x.x.x 
x.x.x.x 
x.x.x.x 

Try:

perl -lne 'print $& if /(\d+\.){3}\d+/' file.xml

thanks! It looks like it did the job. Just after I posted it I found another way with grep. When I count the lines I get more lines with the grep output. Guess it would be difficult to determine without seeing the actual XML output but any ideas for what the reason could be?

grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' blah_text.txt | sort -u |wc -l
395

perl -lne 'print $& if /(\d+\.){3}\d+/' blah_text.txt |sort -u |wc -l
372

Which addresses does this "comm" command print?

grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' blah_text.txt | sort -u > tmp1
perl -lne 'print $& if /(\d+\.){3}\d+/' blah_text.txt |sort -u > tmp2
comm -23 tmp1 tmp2

@Bartus11 I sent you a PM

I think it is happening when there are more than one IP addresses in single line. Try this:

perl -lne 'while (/(\d+\.){3}\d+/g){print $&}' blah_text.txt |sort -u |wc -l

Awesome, your script is 99% there. Your script printed 396 lines. I printed the two out again to tmp1 tmp2 and ran them through diff.

Your script printed the following extra line. As you know an IP address cannot end with four characters. I looked at the original xml and it is an incorrect comment added by an administrator.

10.3.202.1821

Thx for your help

xargs -n1 <infile | sed '/[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*/!d'

... or ... less secure but shorter to write for lazy fingers :

xargs -n1 <infile | sed '/.*\..*\..*\..*/!d'

This is why copy and paste was invented:)