Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

extracting IP adresses from a file 3

Status
Not open for further replies.

czarj

Technical User
Apr 22, 2004
130
0
0
US
I have a file containing the following information I need to extract only the IP addresses from this file and move them to another file. Any suggestions?

vfiler1 running
ipspace: default-ipspace
IP address: 123.123.123.123 [unconfigured]
Path: /vol/palermo [/etc]
UUID: 4fa0d3d8-ecd6-11df-af94-00a09808463c

vfiler2 running
ipspace: default-ipspace
IP address: 12.114.121.88
Path: /vol/palermo [/etc]
UUID: 4fa0d3d8-ecd6-11df-af94-00a09808463c

vfilertest running
ipspace: default-ipspace
IP address: 192.168.3.21
Path: /vol/palermo [/etc]
UUID: 4fa0d3d8-ecd6-11df-af94-00a09808463c


I think this should suffice for finding IP's, but how to implement it?

Code:
[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
I was thinking something like this utilizing the regex code form above, but i'm unsure.

Code:
   open (FaHout, ">> $vfile2"); #open file for writing
   open (FaH, "$vfile"); #read main file
   while ( <FaH> ) {
     chomp;
     my @name = ???;
     print FaHout "$name[0]\n";  #print out IP list

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
How about this (untested)?

I modified your regex as follows:
1) put it inside brackets to get the IP address into variable $1.
2) back-slashed the "." so that the regex looks for periods.

Code:
open (FaHout, ">> $vfile2"); #open file for writing
open (FaH, "$vfile"); #read main file

while ( <FaH> ) {
  chomp;  # strip off linefeed

  if (m/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}/) {
     $ip=$1;
     print FaHout "$ip\n";  #print out IP list
  }
} # while( <FaH> )

If efficiency is important you can improve the regex by using the "^" anchor. So you can change your regex to this:

Code:
if (m/^IP address: ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}/)
 
Something about this doesn't work. I doesn't give me any output.

Code:
#! /usr/software/bin/perl
        open (FaHout, ">> /tmp/vflist2.out");
                open (FaH, "/tmp/vflist1.out");
                        while ( <FaH> ) {
                        chomp;
                                if (m/{[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}}/) {
                                $ip=$1;
                                print FaHout "$ip\n";
                                }
                        }
                close FaH;


Also, one caveot. My file is always going to have a section called "vfiler0." I would exclude any IP adresses listed under vfiler0.

File:
vfiler0 running
ipspace: default-ipspace
IP address: 10.42.156.16 [e0a]
IP address: 10.42.156.19 [bae_test]
IP address: 10.42.156.18 [e5a]
Path: / [/etc]
UUID: 00000000-0000-0000-0000-000000000000

vfiler1 running
ipspace: default-ipspace
IP address: 123.123.123.123 [unconfigured]
Path: /vol/palermo [/etc]
UUID: 4fa0d3d8-ecd6-11df-af94-00a09808463c

vfilerbae running
ipspace: default-ipspace
IP address: 123.124.123.123 [unconfigured]
Path: /vol/bae_testing [/etc]
UUID: 605f7aa8-ecd6-11df-af94-00a09808463c


Desired Output File:
123.123.123.123
123.124.123.123

Thanks!

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
Based solely on the section of the file that you posted (in other words, there's no way to know what the rest of the file looks like) something like this might work for you:
Code:
my $section;

while (<DATA>) {
	$section = $1 if m/^(\S+)\s+running/i;
	if (m/ip address: ((\d+\.){3}(\d+))/i) {
		next if $section eq 'vfiler0';
		print "$1\n";
	}
}
__DATA__
vfiler0                          running
   ipspace: default-ipspace
   IP address: 10.42.156.16 [e0a]
   IP address: 10.42.156.19 [bae_test]
   IP address: 10.42.156.18 [e5a]
   Path: / [/etc]
   UUID: 00000000-0000-0000-0000-000000000000

vfiler1                          running
   ipspace: default-ipspace
   IP address: 123.123.123.123 [unconfigured]
   Path: /vol/palermo [/etc]
   UUID: 4fa0d3d8-ecd6-11df-af94-00a09808463c

vfilerbae                        running
   ipspace: default-ipspace
   IP address: 123.124.123.123 [unconfigured]
   Path: /vol/bae_testing [/etc]
   UUID: 605f7aa8-ecd6-11df-af94-00a09808463c
You will want to replace <DATA> with your file handle. Also, the information including (and below) __DATA__ is not necessary for your script.
 
This works very nicely. Thanks for all the help

Code:
open (OUTDATA, ">> $vfile2") or die "Could not open $vfile2: $!"; 
		open (GETDATA, "$vfile") or die "Could not open $vfile: $!";
		my $vfiler0 = 0;
		my $vfiler = 0;
		while(<GETDATA>)
		{
		chomp;
		if(/^\s*$/)
			{
			$vfiler = 0;
			}
		if(/vfiler0/)
			{
			$vfiler = 1;
			}
		if (!$vfiler && m/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/)
			{
			my $ip=$1;
			print OUTDATA "$ip\n";
			}
		}
		close OUTDATA;
		close GETDATA;

--- You must not fight too often with one enemy, or you will teach him all your tricks of war.
 
I just cleaned up one section, no change in logic
Code:
if (!$vfiler && m/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/) {            
print OUTDATA "$1\n";            
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top