Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Extracting blocks of text

by Rhose (Priest)
on Jan 30, 2004 at 14:38 UTC ( #325239=note: print w/ replies, xml ) Need Help??


in reply to Extracting blocks of text

You could also use the range (flip-flop) operator. The sample below will print lines from the line which starts with "head" (^ anchors to the start) to the one which ends with "tail" (\s*$ allows some white space after tail.)

#!/usr/bin/perl use strict; use warnings; while(<DATA>) { print if /^head/i../tail\s*$/i; } __DATA__ HEAD gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla tail gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus head bla bla gugus gugus tail bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus head bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus tail gugus gugus

Output

HEAD gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla tail gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus gugus bla bla gugus head bla bla gugus gugus tail

Update
If you have the camel book, you can find a discussion on this starting on page 90 (2nd Edition).


Comment on Re: Extracting blocks of text
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-09-20 13:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls