in reply to Reading a pattern and a number of lines after it

Shenanagians! I call Shenanagians! blakem broke the rules ;)

The original stipulations stated that the arguments would be supplied via command line; here is the shortest I came up with with that:

open(T,"$ARGV[0]"); foreach(@_=<T>){/$ARGV[1]/?print@_[$c..$ARGV[2+$c],exit:$c++} close(T)

Unfortunately, much space is taken up by the $ARGV. Also, note that by changing the exit to a "\n" (or removing it completely) will cause the code will print $ARGV[2] lines for ALL matches. Leaving the exit causes only the first batch to print.

Update:
I can cheat too (using blakem's arguments as an example):

perl -ne "print((<>)[0..3])if/ou/" test.txt
and completely floor him; but, as I said, that would be cheating ;)

Replies are listed 'Best First'.
Re (tilly) 2: Reading a pattern and a number of lines after it
by tilly (Archbishop) on Jan 09, 2002 at 06:25 UTC
    You didn't print the line that the match was on, which was stipulated. I also don't like having it wait forever for more input files. A better cheating solution is therefore:
    perl -ne'die$_,(<>)[0..3]if/ou/' test.txt
    (Erm, OK. I may have just printed to the wrong filehandle... :-)
      I stand corrected; your cheating solution through the use of additional cheating is much better than mine! I just whipped mine up real quick to spite blakem, I didn't check it as thoroughly as I should :)