http://qs321.pair.com?node_id=980408


in reply to Getting lines in a file between two patterns

I'll show you another common way that this parsing problem is handled. May be useful in other situations...

When you see the "start-of-record", call a subroutine to process the record. In this case there is no state variable to say that "we are in the record", the fact that you are in the process_record() subroutine serves that purpose.

#!/usr/bin/perl -w use strict; while (<DATA>) { process_record() if /^START/; } sub process_record { my $line; while ($line = <DATA>, $line !~ /^END/) { print "$line" } print "\n"; #a printout spacer for next record } =prints These are the first set of lines which are to be extracted These are the second set of lines which are to be extracted =cut __DATA__ XXXX YYYY START These are the first set of lines which are to be extracted END XXX ZZZ YYY START These are the second set of lines which are to be extracted END aasds tteret tertetr