Skip to content
Prev 265129 / 398502 Next

grep lines before or after pattern matched?

Try this (untested as I'm on my iPhone now):

index <- grep("Document+.", yourfile, value = FALSE)
index <- c(index + 2, index + 4)

You just need to make sure you avoid recycling, e.g.,

1:10 + c(2, 4) # not what you want

If you want a sufficient number of lines that manually writing index + becomes cumbersome, you could use something like:

as.vector(sapply(c(2, 4), "+", e2 = index))

HTH,

Josh
On Jul 11, 2011, at 11:09, Simon Kiss <sjkiss at gmail.com> wrote: