Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Getting lines in a file between two patterns

by Marshall (Canon)
on Jul 07, 2012 at 01:01 UTC ( #980408=note: print w/replies, xml ) Need Help??


in reply to Getting lines in a file between two patterns

I'll show you another common way that this parsing problem is handled. May be useful in other situations...

When you see the "start-of-record", call a subroutine to process the record. In this case there is no state variable to say that "we are in the record", the fact that you are in the process_record() subroutine serves that purpose.

#!/usr/bin/perl -w use strict; while (<DATA>) { process_record() if /^START/; } sub process_record { my $line; while ($line = <DATA>, $line !~ /^END/) { print "$line" } print "\n"; #a printout spacer for next record } =prints These are the first set of lines which are to be extracted These are the second set of lines which are to be extracted =cut __DATA__ XXXX YYYY START These are the first set of lines which are to be extracted END XXX ZZZ YYY START These are the second set of lines which are to be extracted END aasds tteret tertetr

Replies are listed 'Best First'.
Re^2: Getting lines in a file between two patterns
by darklord_999 (Acolyte) on Jul 11, 2012 at 19:25 UTC
    Thanks Marshall. I agree this is actually a much better solution and suits my need perfectly.
Re^2: Getting lines in a file between two patterns
by Anonymous Monk on Jun 27, 2016 at 07:42 UTC

    I made some experiment with these lines of codes and maybe I find an issue (I'm not a perl expert...). If input file is like below, with two START-END sections in succession, perl script seems to skip the "START" line after the previous "END" ones, and so it doesn't print the content of the paragraph. I can't say which can be the reason for this behaviour

    __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

      I am unable to reproduce your findings. Here are the results with perl 5.20.3:

      $ cat 980408.pl #!/usr/bin/perl -w use strict; while (<DATA>) { process_record() if /^START/; } sub process_record { my $line; while ($line = <DATA>, $line !~ /^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 $ ./980408.pl These are the first set of lines which are to be extracted New line And new Will be extracted? These are the second set of lines which are to be extracted $
      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.
        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

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://980408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2020-12-01 00:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?