When dealing with multi-line data, there are two basic possibilities: Either (1) read the entire file into memory (5000 lines is not that big, especially if the lines are only as short as you've shown) and apply a regex over the whole thing, or (2) read the file line-by-line and keep some kind of state, keeping in mind that you're only looking at one line at a time (Update: or, if the state you are keeping is a buffer, like a sliding window, a few lines). This second point is why your code isn't working: You're expecting your regex $beg to match two lines since you've included a \n in your regex, but if you're reading the file line-by-line, you'll only ever have one line in $_ at a time (or $line, that's unclear from your code).

The downside of the first approach is that it uses more RAM and will start being problematic if your files grow. Here's an example:

use warnings; use strict; my $data = do { open my $fh, '<', 'sample.txt' or die $!; local $/; <$fh> }; # slurp whole file into memory my $regex = qr{ ^ \s*APP>\sSTART_PARTU_8X8 \n ^ \s*APP>\sPARTITION_SPLIT \n (.*?) ^ \s*APP>\sEND_PARTU_8X8 $ }msx; if ($data=~$regex) { print $1; }

The downside of the second approach is that it makes the logic a bit more complex, but I tend to prefer it since it allows for any size input file, and it can be (IMO) more easily customized and extended for more complex state changes. Here's an example:

use warnings; use strict; my $beg1 = qr/^\s*APP>\sSTART_PARTU_8X8$/; my $beg2 = qr/^\s*APP>\sPARTITION_SPLIT$/; my $end = qr/^\s*APP>\sEND_PARTU_8X8$/; open my $fh, '<', 'sample.txt' or die $!; my $prev_line; my $state = 'not_in_block'; while ( my $line = <$fh> ) { if ( $state eq 'not_in_block' ) { if ( $prev_line && $prev_line=~$beg1 && $line=~$beg2 ) { $state = 'am_in_block'; } } elsif ( $state eq 'am_in_block' ) { if ( $line=~$end ) { $state = 'not_in_block'; } else { print $line; } } } continue { $prev_line = $line } close $fh;

Using the sample input data from your post as sample.txt, both pieces of code output:

START_PARTU_4X4 ........ START_PARTU_4X4 ........ START_PARTU_4X4 ........ START_PARTU_4X4 ........

Of course, this being Perl, there are other ways to approach this, but these are two pretty common ones.


In reply to Re: Print lines between multi-line regex pattern by haukex
in thread Print lines between multi-line regex pattern by Eshan_k

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.