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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Getting lines in a file between two patterns
by darklord_999 (Acolyte) on Jul 11, 2012 at 19:25 UTC | |
Re^2: Getting lines in a file between two patterns
by Anonymous Monk on Jun 27, 2016 at 07:42 UTC | |
by hippo (Bishop) on Jun 27, 2016 at 08:28 UTC | |
by Marshall (Canon) on Jun 27, 2016 at 15:29 UTC | |
by Marshall (Canon) on Apr 23, 2018 at 20:15 UTC | |
by Anonymous Monk on Apr 23, 2018 at 06:31 UTC | |
by poj (Abbot) on Apr 23, 2018 at 07:07 UTC | |
by Anonymous Monk on May 17, 2018 at 18:17 UTC | |
|
In Section
Seekers of Perl Wisdom