sed read X lines after match

I want to do something like sed -n '/PATTERN/,+10p' and get the ten lines following PATTERN. However, this throws an "expected context address" with the sed that comes with OSX Lion. If that + is a GNUism, can I do this, or do I have to find another tool?

10 following the pattern, excluding the pattern:

awk 'c&&c--;/pattern/{c=10}' infile

10 following the pattern, including the pattern:

awk '/pattern/{c=11};c&&c--' infile

The obvious way is to match a line, then use the "n" command to read more lines and finally output them with the "p" command (adjust the number of "n"s to match your goal):

sed -n '/<match-regexp>/ { n, n, p }' /path/to/infile

Another possibility would be a "range clause": ranges start with a starting regexp and end either with an ending regexp or a line number, which can be absolute (simply a number) or relative to the file position. This is what ".,$ ..." is: from the current line (".") to the end of the file ("$"). The blueprint is:

/<start-regexp>/,/<end-regexp>/ {cmd, cmd, ... }

It is possible in "vi" to use relative line numbers like you did:

/<regexp>/,+10 {cmd}

but i doubt this is standard BRE (would have to consult the reference, which i haven't access to right now). The above solution is guaranteed to work with every "sed", though.

By the way, check the man page of your "grep". Some versions have the feature you look for: they can match a line and then output a configurable mount of lines before and/or after the match.

I hope this helps.

bakunin