http://qs321.pair.com?node_id=1166668


in reply to Re^2: Getting lines in a file between two patterns
in thread Getting lines in a file between two patterns

In order to reproduce your symptoms, if one of the START tokens doesn't start in column 1 (right at the beginning of the line), then then it wouldn't be recognized and that record would be skipped.

I made some slight changes to allow for START and END not being at the beginning of the line. Also, I eliminated the use of the "comma operator" in the sub's while statement in favor of an "and" statement. This should handle the case of a malformed record missing an "END" at the end of the DATA a bit better.

Your DATA as posted does indeed work as hippo demonstrates. This code should work on any Perl >= 5.6 - nothing "fancy" about it.

This is a very exacting business and when making a bug report, please post exactly the DATA that causes the problem, verbatim.

#!/usr/bin/perl -w use strict; while (<DATA>) { process_record() if /^\s*START/; } sub process_record { my $line; while (defined ($line = <DATA>) and $line !~ /^\s*END/) { print "$line" } print "\n"; #a printout spacer for next record } __DATA__ XXXX YYYY START These are the first set of lines which are to be extracted END START New line And new Will be extracted? END XXX ZZZ YYY START These are the second set of lines which are to be extracted END aasds tteret tertetr
As a comment, this malformed "START" record problem applies to all of the code in this thread. This is not unique to my code. "  START" vs "START" is my best guess as what you did wrong in your DATA.

Replies are listed 'Best First'.
Re^4: Getting lines in a file between two patterns
by Marshall (Canon) on Apr 23, 2018 at 20:15 UTC
    Adapt my code like this:
    #!/usr/bin/perl use warnings; use strict; my @current_record; while (<DATA>) { process_record() if /^\s*START/; } foreach (@current_record) { print; } sub process_record { my $line; @current_record=(); while (defined ($line = <DATA>) and $line !~ /^\s*END/) { push @current_record, $line; } } __DATA__ XXXX YYYY START These are the first set of lines which are to be extracted END START These are the second set of lines which are to be extracted END XXX ZZZ YYY START These are the third set of lines which are to be extracted END aasds tteret tertetr
Re^4: Getting lines in a file between two patterns
by Anonymous Monk on Apr 23, 2018 at 06:31 UTC

    Hi,

    How can I get only the last set of pattern matched lines.

    INPUT __DATA__:

    XXXX YYYY START These are the first set of lines which are to be extracted END START These are the second set of lines which are to be extracted END XXX ZZZ YYY START These are the third set of lines which are to be extracted END aasds tteret tertetr

    OUTPUT:

    These are the third set of lines which are to be extracted

    Thanks,

    Gopi

    2018-04-23 Athanasius added code and paragraph tags

      Push lines to an array, clear it at each START.

      #!/usr/bin/perl use strict; my $flag = 0; my @lines = (); while (<DATA>){ if (/^\s*END/){ $flag = 0 } elsif (/^\s*START/){ $flag = 1; @lines = (); } elsif ($flag) { push @lines,$_; } } print join '',@lines;
      poj

        Hi,

        How can I get only the first/nth set of pattern matched lines.

        INPUT __DATA__:

        XXXX YYYY START These are the first set of lines which are to be extracted END START These are the second set of lines which are to be extracted END XXX ZZZ YYY START These are the third set of lines which are to be extracted END aasds tteret tertetr [download]

        OUTPUT:

        These are the first set of lines which are to be extracted

        2018-05-18 Athanasius added code and paragraph tags