Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Getting lines in a file between two patterns

by Yates (Beadle)
on Jul 05, 2012 at 08:33 UTC ( [id://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 && $_ !~ /^$/)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 05:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found