Jose Malacara wrote:
> Can someone please provide some assitance with a multi-line matching
> problem? I have a datafile that looks like this:
> 
> #### Input file ########
> START
> foo1
> bar1
> END
> START
> foo2
> bar2
> END
> START
> foo3
> bar3
> END
> 
> I am trying to capture the contents between the START and END
> delineators. Here is what I have so far:
> 
> #### Script ########
> #!/usr/bin/perl
> $input_file = $ARGV[0];
> open (INPUT, "<$input_file") or die "Cannot open $input_file file: $!";
> local $/;
> while (<INPUT>){
>    if ( $_ =~ m/^\s*START\s+(.+?)\s+END$/smi ) {
>       print "$1\n";
>    }
> }
> close (INPUT);
> 
> However, when I run the script it only appears to match the START/END
> sequence the first time.  This is what I get:
> 
> #### Result ########
> # ./test.pl file
> foo1
> bar1
> 
> This is what I'm trying to achieve:
> # ./test.pl file
> foo1
> bar1
> foo2
> bar2
> foo3
> bar3
> 
> Is the While loop required since I am actually slurping the file
> contents into a single variable? Is there a (better) mechanism to
> continue matching against the string if its matched once?  Any help
> would be greatly appreciated!

Here is one way to do it:

#!/usr/bin/perl
use warnings;
use strict;

$input_file = shift;
open INPUT, '<', $input_file or die "Cannot open $input_file file: $!";

local $/ = "END\n";
while ( <INPUT> ) {
    s/.*START\n//;
    print;
    }

close INPUT;

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to