in reply to Re: grep -A10 oneliner?
in thread grep -A10 oneliner?

It fails in two fashions. It only displays 9 lines after, not 10, it doesn't support overlapping ranges.

Using the input file from 447941, I get:

> perl -ne "print if $.=/pattern/..10" !.txt pattern j k l pattern 1 2 3 4 5 6

and not

pattern j k l pattern 1 2 3 4 5 6 7 8 9 10

Replies are listed 'Best First'.
Re^3: grep -A10 oneliner?
by Roy Johnson (Monsignor) on Apr 14, 2005 at 20:34 UTC
    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.
      And one charater less
      perl -ne 'print if$c=/pattern/?11:$c-!!$c'
      -- gam3
      A picture is worth a thousand words, but takes 200K.