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.