[R] grep lines before or after pattern matched?

Simon Kiss sjkiss at gmail.com
Mon Jul 11 18:31:01 CEST 2011


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 how to grep the newspaper name.  There is no field ID attached to those lines. The best I can come up with would be to have the program grep the four lines following matching the pattern "Document [0-9]".  There is an an argument to grep in unix that can do this ...grep -A4 'pattern' infile>outfile, but I don't know if there is an equivalent argument in R.

Any thoughts.
Yours, Simon Kiss
*********************************
Simon J. Kiss, PhD
Assistant Professor, Wilfrid Laurier University
73 George Street
Brantford, Ontario, Canada
N3T 2C9
Cell: +1 905 746 7606



More information about the R-help mailing list