in reply to grep -A10 oneliner?

As I posted the other day: perl -ne 'print if $.=/pattern/..10' Update: using // instead of ?? (originally thought you only wanted the first match)

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: grep -A10 oneliner?
by ikegami (Patriarch) on Apr 14, 2005 at 20:21 UTC

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

      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.