Extract list of IP addresses from a text file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract list of IP addresses from a text file.
# 1  
Old 03-01-2011
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.


Code:
 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

Code:
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

# 2  
Old 03-01-2011
Try:
Code:
perl -lne 'print $& if /(\d+\.){3}\d+/' file.xml

# 3  
Old 03-01-2011
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?

Code:
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


Last edited by Franklin52; 03-02-2011 at 03:24 AM.. Reason: Please use code tags
# 4  
Old 03-01-2011
Which addresses does this "comm" command print?
Code:
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

# 5  
Old 03-01-2011
@Bartus11 I sent you a PM
# 6  
Old 03-01-2011
I think it is happening when there are more than one IP addresses in single line. Try this:
Code:
perl -lne 'while (/(\d+\.){3}\d+/g){print $&}' blah_text.txt |sort -u |wc -l

# 7  
Old 03-01-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. UNIX for Dummies Questions & Answers

List Contacted Web Addresses?

Greetings. I'm in the process of tracking down an issue with Firefox, and need some way of gathering the actual web addresses which are connected to from localhost. The specific problem centers around the determination/capture of the exact generated app.update.url address formed by the... (2 Replies)
Discussion started by: LinQ
2 Replies

4. Shell Programming and Scripting

Extract e-mail addresses on a page

Hi I normally ask questions on coding but I think there is a code that can do this. I have regular text throughout my file and I want to extract all e-mail addresses from it (rather than going and searching each one). E-mails all have @ so I assume there is a way. Thanks Phil (6 Replies)
Discussion started by: phil_heath
6 Replies

5. Shell Programming and Scripting

Formatting list of IP addresses into a grep command

Hi I have an input file with a list of random IP addresses, each on a new line. Below is just an example as I omitted the real IP addresses for obvious reasons. Input: random_ip.txt 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1 10.0.0.1... (7 Replies)
Discussion started by: lewk
7 Replies

6. UNIX for Dummies Questions & Answers

analyzing list with street addresses

Hi List, Could someone please point me into the right direction with the following: I have a file containing a list of street addresses. I need to sort all the street addresses with the same number to a new file containing the street name and corresponding number. So: Strawinskylaan... (3 Replies)
Discussion started by: M474746
3 Replies

7. Shell Programming and Scripting

extract text from a file

I have been reading several posts regarding how to extract text from a file, but none of those have helped me for what I need. This is my problem: I need to extract the text after my pattern So my line is: 485.74 6589.5 Log likelihood: 1485.79 My pattern is 'Log likelihood:' and I need... (2 Replies)
Discussion started by: loperam
2 Replies

8. Shell Programming and Scripting

Extract IP addresses

The only way I could extract the user names and 'from' IP addresses is to use a few temp files. Split up by 'Failed keyboard-interactive' and 'Failed password'. Anyone have any idea to do this all in one go? aaa.bbb.ccc.ddd 2009-03-23 01:28:33 sshd: Failed keyboard-interactive/pam... (2 Replies)
Discussion started by: hazno
2 Replies

9. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

10. UNIX for Dummies Questions & Answers

Help extracting MAC addresses from List

Hello all. I have a large number of text files outputted from various Netstumbler Wireless Scans; from which I need to extract the MAC addresses of the various Access Points. The Text files look like this: # $Creator: Network Stumbler Version 0.4.0 # $Format: wi-scan summary with... (9 Replies)
Discussion started by: dhs23
9 Replies
Login or Register to Ask a Question

Featured Tech Videos