in reply to Grep - print matched line and next N lines

You haven't allowed for overlapping regions. If I want to print out any line matching "foo" plus the next 3 lines in the following
qux foo bar 1 bar 2 foo bar 1 bar 2 bar 3 qux

Then I'd want all the lines printed from the first "foo" to the last "bar", yours only prints up to the second "foo" line.

Replies are listed 'Best First'.
Re: Re: Grep - print matched line and next N lines
by runrig (Abbot) on Aug 13, 2002 at 00:08 UTC
    You haven't allowed for overlapping regions...

    I did consider it, and decided I didn't want/need that behavior, though it would make for another nifty command line option if that is the behavior you want. Feel free to patch...

    Update: Ok, its updated, though I suppose now you'll want an option to print lines from the next file if the match is at the end of the current file... :-) (though that would be easy, I think replacing the eof line with this would do it):

    last if $opt_f ? eof() : eof;
      You make it sound like that behavior is non-standard in some way, but it is exactly what standard GNU grep does for the -A (--after-context) option and is typically what people would expect if you say your grep will print N lines following each matching line.
        ..but it is exactly what standard GNU grep does ..

        Ahh, well that explains it. Although I've heard GNU grep has this option, I don't have GNU grep on my system, so I'm not familiar with exactly how it behaves (and not having GNU grep is why I wrote this in the first place). Anyway, I've reversed the meaning of the option-r to make your suggested behavior the default :-)