in reply to Print lines between multi-line regex pattern

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.

Replies are listed 'Best First'.
Re^2: Print lines between multi-line regex pattern
by Eshan_k (Acolyte) on Jun 21, 2018 at 23:59 UTC
    Thank you haukex for your suggestion. I really appreciate your help.