in reply to Extracing (and excluding) IP's from a file
You'd be much more likely to read the file reliably if you take advantage of the format and keywords.
If you look for the line storing IP addresses (the ones beginning with "IP address" you are guarenteed to get all of the IP addresses. On the other hand, if you scan for what matches an IP4 syntax, you have two risks. (1) you are just crossing your fingers that only lines that actually have IP addresses store values that look like IP addresses. (2) I'd also note that your IP address pattern assume IP4, but can you really be sure you won't have a few entries in IP6 format?
I'm guessing from your sample that your format rules look something like this:
To pick out IP addresses based on attribute names, you would use a very simple state machine that determines the current record based on the value of $sId and the current attribute based on the value of $sAttribute, like this:
use strict; use warnings; my $sId=''; my $sAttribute=''; my $sValue=''; while (my $sLine = <DATA>) { chomp $sLine; #figure out the id of the current record if ($sLine !~ /^\s*$/) { # $sId is '' when we read the first line if (!$sId) { # first line of record contains id, e.g. vfiler0, vfiler1, etc # id is first run of non-white characters - store it in $sId ($sId) = ($sLine =~ /^\s*(\S*)/); } #skip all vfiler0 record lines next if ($sId eq "vfiler0"); #get IP addreses for other records if attribute is "IP Address" #attribute name goes from first non-white character to first ':' ($sAttribute, $sValue) = ($sLine =~ /^\s*(\S[^:]*):\s*(\S*)/); if ($sAttribute && ($sAttribute eq 'IP address') && $sValue) { print "$sValue\n"; } } elsif ($sLine =~ /^\s*$/) { # records divided by entirely blank lines # when not in record set id to '' $sId=''; } #print STDERR "line ($sId): <$sLine>\n"; }
|
|---|