http://www.perlmonks.org?node_id=984204

diamondsandperls has asked for the wisdom of the Perl Monks concerning the following question:

I need to regex the next line of a given match and have contents of next line which will be a domain or ip address assign as a variable. The match will either be a domain or an ip on the external ip match on the internal it will always be an ip. The match will be after the headings. Some domains will have www. at the beginning and i need to get rid of this and just have domain.com or .net or whatever


#!perl use strict; use warnings; use DateTime; use LWP::UserAgent; use HTML::Tree; my $newline; my $textfile_new; my @textfiles = <*.txt>; foreach my $textfile (@textfiles) { if ($textfile =~ /(\d+.\d+.\d+.\d+.txt)/) { my ($output_file) = $textfile =~ /(\d+.\d+.\d+.\d+).txt/; open(my $input_fh, '<', $textfile) or die "Failed to open $textfile: $!"; open(my $output_fh, '>', "${output_file}_report.txt") or die "Failed to open ${output_file}_report.txt: $!"; print $output_fh "my description of stuff.\n\n"; my $ua = new LWP::UserAgent; $ua->timeout(10); #10 second timeout $ua->proxy(['http'], 'http://mywebproxy:8080'); while ( <$input_fh> ) { print $output_fh $_; #need to regex for the following then use LWP to get d +ata based on the IP on the next line. my $internal_ip =~ /Internal IP\n(\d+.\d+.\d+.\d+)/; my $external_ip =~ /SIP\(s\)\n(\d+.\d+.\d+.\d+)/; #lwp stuff my $response_internalip = $ua->get("http://myinternali +p_referentialdatasearch"); my $content_internalip = $response_internalip->content +; my $tree_internalip = HTML::Tree->new(); $tree_internalip->parse($content_internalip); print {$output_fh} $tree_internalip, "\n"; my $response_externalip = $ua->get("http://myexternali +plookup/phpwhois-4.2.2/example.php?query=$external_ip&output=normal") +; my $content_externailip = $response_externalip->conten +t; my $tree_externalip = HTML::Tree->new(); $tree_externalip->parse($content_externalip); print {$output_fh} $tree_externalip, "\n"; } } }

Replies are listed 'Best First'.
Re: trouble regexing the next line
by moritz (Cardinal) on Jul 28, 2012 at 14:30 UTC
      Thanks man yeah reading the entire file in is what i needed thanks for the insight
Re: trouble regexing the next line
by Kenosis (Priest) on Jul 28, 2012 at 15:20 UTC

    If you use Tie::File;, you can tie the file to an array, and then iterate over the array's elements (i.e., the file's lines) using something like for(my $i = 0; $i < scalar @array; $i++){ ... }, looking for a pattern match in each $array[$i], where $array[$i+1] would be the next line in the file (provided $i+1 < scalar @array).

Re: trouble regexing the next line
by cheekuperl (Monk) on Jul 28, 2012 at 14:56 UTC
    i am a bit slow at analysing existing code.
    Could you please post some sample input and expected output?