Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: [Solved]Need to extract a particular block of lines between two patterns

by Cristoforo (Curate)
on Nov 09, 2017 at 03:34 UTC ( #1202995=note: print w/replies, xml ) Need Help??


in reply to [Solved]Need to extract a particular block of lines between two patterns

This solution makes use of the next if 1 .. /^END$/; flip flop operator. The 1 is the first line of the file to the first END. The next time it encounters a START END block, it performs the actions in the code.
#!/usr/bin/perl use strict; use warnings; while (<>) { next if 1 .. /^END$/; if (/^START$/ .. /^END$/) { next if /^START$/; last if /^END$/; print; } }
This works for the data sample you provided.

Replies are listed 'Best First'.
Re^2: [Solved]Need to extract a particular block of lines between two patterns
by chengchl (Novice) on Nov 11, 2017 at 01:38 UTC

    Hi Cristoforo,

    Thank you so much for your help. Please correct me if I understand it wrong - the code will skip the first START ... END pattern but will output the all the following patterns right? That is to say, the third START .. END pattern will be printed out as well even if it's not wanted?

    Thank you so much for the help again!

      Yes, it will skip the first block. It will exit the while loop (last if /^END$/) when it reaches the END for the second block.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1202995]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2017-12-16 10:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (449 votes). Check out past polls.

    Notices?