The first is trivial: set the number to be the total number of context lines you want, including the matched pattern line.
The second is a little trickier:
perl -ne 'print if $.=/pattern/?1:2..10'
In this case, it will print 10 lines after the match.
Update: so tricky, in fact, that you can't do it with the range operator. So there's no point in using $.. So you just do
perl -ne 'print if$c=/pattern/?11:$c&&$c-1'
or any of the other solutions here. Sigh.
One that doesn't require you to add one to the number you put in:
perl -ne 'print if(/pattern/?$c=10:$c--)>0'
Caution: Contents may have been coded under pressure.
|