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

In reply to Print all lines between to keywords by perlnewbie9292

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.