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

Hello Perl experts, I am pretty new to Perl and I am with no luck been trying to print all lines between two keywords that I have defined.

Not sure what the heck I am doing wrong.

I have a variable "$linesFromFile" which contains all data from a file. If I print the $linesFromFile I get correct results. Now what I am not getting results for is I am trying to print all lines between the two strings shown below, sometimes the (start and end) strings only appear once, sometimes many times. What I am trying to do is print all lines for every time the two strings are found.

Not sure how to on match and save all lines between to search strings. I tried the save () and print $1 with no luck also.

Thanks for the help in advance.
for ( $linesFromFile ) { if ( /^StartTr1/gm .. /^oflTr1/gm ) { print; } }
SAMPLE FILE
StartTr1 sample 1 sample 1 sample 1 sample 1 SAMPLE 1 oflTr1 nothing nothing nothing StartTr1 sample 2 sample 2 sample 2 sample 2 sample 2 last line before sample 2 oflTr1 junk junk junk
RESULTS DESIRED
sample 1 sample 1 sample 1 sample 1 SAMPLE 1 sample 2 sample 2 sample 2 sample 2 sample 2 last line before sample 2

Replies are listed 'Best First'.
Re: Print all lines between to keywords
by AnomalousMonk (Archbishop) on Nov 09, 2009 at 22:03 UTC
    This is a solution that depends on the fact that range operators return a 'sequence number' (see Range Operators in perlop). The advantage, such as it is, is that only one (possibly expensive) regex match is performed per line, the numeric comparison and the match against  /E0$/ being assumed to be insignificantly cheap. The file  806007.dat contains the data from the OP plus a few  'ignore ...' lines at the very beginning.
    >perl -wMstrict -e "open my $fh, '<', '806007.dat' or die qq{opening: $!}; my $linesFromFile = [ <$fh> ]; close $fh or die qq{closing: $!}; for (@$linesFromFile) { next unless my $seq_n = /^StartTr1/ .. /^oflTr1/; print unless $seq_n < 2 or $seq_n =~ /E0$/; } " sample 1 sample 1 sample 1 sample 1 SAMPLE 1 sample 2 sample 2 sample 2 sample 2 sample 2 last line before sample 2
    Note that the  while (<DATA>) { ... } form given by toolic is preferred for this sort of file processing. I have assumed that there is a need to read the entire file before processing it, as is done in the OP, and have given the code in this form.
Re: Print all lines between two keywords
by toolic (Bishop) on Nov 09, 2009 at 20:22 UTC
    To exclude the start and end text...
    use strict; use warnings; while (<DATA>) { if ( /^StartTr1/ .. /^oflTr1/ ) { print unless (/^StartTr1/) or (/^oflTr1/); } } __DATA__ StartTr1 sample 1 sample 1 sample 1 sample 1 SAMPLE 1 oflTr1 nothing nothing nothing StartTr1 sample 2 sample 2 sample 2 sample 2 sample 2 last line before sample 2 oflTr1 junk junk junk

    prints:

    sample 1 sample 1 sample 1 sample 1 SAMPLE 1 sample 2 sample 2 sample 2 sample 2 sample 2 last line before sample 2