in reply to grab 'n' lines from a file above and below a /match/

I have a rather stupid question

There are no stupid questions, only stupid ways of asking them, and you didn't do that so that's OK.

The solution you are looking for is, I suspect, to read a line at a time, populating an array of 2n+1 entries (n above the line, plus the line, plus n below the line). Once the array is full, shift the first entry out of it, and push a new entry onto the end. When the *middle* entry in the array matches your desired string, dump the whole array to the screen. Something like this ...

open(FILE, 'file') || die("Yaroo!\n"); my $N = 3; # 3 lines above and below my $target = 'stuff what you want'; my @window = ('') x ($N + $N + 1); while(<FILE>) { shift @window; push @window, $_; print @window, "\n\n" if($window[$N] =~ /$target/); } foreach(1 .. $N) { shift @window; print @window, "\n\n" if($window[$N] =~ /$target/); }
update: I should have explained, the foreach loop copes with the case where the target text appears within the last N lines of the file.