Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Getting lines in a file between two patterns

by Yates (Beadle)
on Jul 05, 2012 at 08:33 UTC ( #979989=note: print w/ replies, xml ) Need Help??


in reply to Getting lines in a file between two patterns

You could use a counter, which is set to 1 when you reach START then reset to 0 when you reach END. As the counter will only be 1 between START and END, you can simply print out any lines where this is the case.

use strict; my $count = 0; open (IN, "input.txt"); while (<IN>) { if (/START/) { $count = 1; } elsif (/END/) { $count = 0; } elsif ($count) { print; } } close IN;
Output:
These are the first set of lines which are to be extracted These are the second set of lines which are to be extracted
I wasn't sure if the blank lines were intentional, but if you don't want them, change to
elsif ($count && $_ !~ /^$/)


Comment on Re: Getting lines in a file between two patterns
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (13)
As of 2014-09-02 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (20 votes), past polls