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.
In reply to Re: Lines above and Below a matched string
by svsingh
in thread Lines above and Below a matched string
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |