[R] grep lines before or after pattern matched?

2011-07-11 Thread Simon Kiss
Dear colleagues, I have a series of newspaper articles in a text file, downloaded from a text file. They look as follows: Document 1 of 100 \n \n \n Newspaper Name \n \n Day Date I have a series of grep scripts that can extract the date and convert it to a date object, but I can't figure out

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Joshua Wiley
Dear Simon, Maybe I don't understand properlyif you are doing this in R, can't you just pick the line you want? Josh ## print your data to clipboard cat(Document 1 of 100 \n \n \n Newspaper Name \n \n Day Date, file = clipboard) ## read data in, and only select the 4th line to pass to

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Simon Kiss
Hi Josh, Sorry for the insufficient introduction. This might work, but I'm not sure. The file that I have includes up to 100 documents (Document 1, Document 2, Document 3Document 100) with the newspaper name following 4 lines below each Document number. I'm using readlines to get the text

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Joshua Wiley
If you know you can find the start of the document (say that line always starts with Document...), then: grep(Document+., yourfile, value = FALSE) + 4 should give you 4 lines after each line where Document occurred. No loop needed :) On Mon, Jul 11, 2011 at 10:25 AM, Simon Kiss

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Simon Kiss
Josh, that's amazing. Is there any way to have it grab two different lines after the grep, say the second and the fourth line? There's some other information in the text file I'd like to grab. I could do two separate commands, but I'd like to know if this could be done in one command... Simon

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Joshua Wiley
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

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Bert Gunter
Simon: Basic basic stuff (not grep -- the stuff thereafter) . Please read the docs, especially the tutorial, An Intro to R. ... and Josh's solution can be shortened to (as he knows): index - grep(Document+., yourfile, value = FALSE) + c(2,4) -- Bert On Mon, Jul 11, 2011 at 11:19 AM, Joshua

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Joshua Wiley
On Jul 11, 2011, at 12:00, Bert Gunter gunter.ber...@gene.com wrote: Simon: Basic basic stuff (not grep -- the stuff thereafter) . Please read the docs, especially the tutorial, An Intro to R. ... and Josh's solution can be shortened to (as he knows): index - grep(Document+.,

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread David Winsemius
On Jul 11, 2011, at 3:33 PM, Joshua Wiley wrote: On Jul 11, 2011, at 12:00, Bert Gunter gunter.ber...@gene.com wrote: Simon: Basic basic stuff (not grep -- the stuff thereafter) . Please read the docs, especially the tutorial, An Intro to R. ... and Josh's solution can be shortened to

Re: [R] grep lines before or after pattern matched?

2011-07-11 Thread Bert Gunter
Josh: (assuming you have interpreted correctly) You are *absolutely* right -- I did not read carefully enough. Does index - matrix(rep(grep(Document+., yourfile, value = FALSE),e=3) + c(0,2,4),nc=3,byr=TRUE) do it for you? Sheepishly, Bert On Mon, Jul 11, 2011 at 12:33 PM, Joshua Wiley