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

Re: Lines above and Below a matched string

by svsingh (Priest)
on Jun 26, 2003 at 19:57 UTC ( [id://269393]=note: print w/replies, xml ) Need Help??


in reply to Lines above and Below a matched string

Here's something a little different. I know you want to match on SOMETHING, but since you'll also need to match on DATE and SOMETHINGELSE (I'm assuming), why not match all three in sequence?

To me, this is a little simpler than the buffer ideas. It probably has some flaws, but it seems to be working.

my $tmp; while ($tmp = <DATA>) { if ($tmp =~ /(DATE\d)/) { my %h = (); $h{'date'} = $1; $tmp = <DATA>; if ($tmp =~ /(SOMETHING\d)/) { $h{'match'} = $1; $tmp = <DATA>; if ($tmp =~ /(SOMETHINGELSE\d)/) { $h{'more'} = $1; print "$h{'match'},$h{'date'},$h{'more'}\n"; } else { redo; } } else { redo; } } } __DATA__ anything anything DATE1 anything anything anything SOMETHING1 anything anything anything SOMETHINGELSE1 anything anything DATE2 anything anything anything DATE3 anything anything anything SOMETHING2 anything anything anything SOMETHINGELSE2 anything anything DATE4 anything anything anything SOMETHING3 anything anything DATE5 anything anything anything SOMETHING4 anything anything anything SOMETHINGELSE3

That returns:

SOMETHING1,DATE1,SOMETHINGELSE1 SOMETHING2,DATE3,SOMETHINGELSE2 SOMETHING4,DATE5,SOMETHINGELSE3

Hope this helps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-24 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found