Dear R-help guys, I am trying to extract two particular words from different files but I am struggling with the code. The first theree lines of each folder are as follows: # using minimal fraction of valid points 30.00 # tas [K] from bcc-csm1-1 model output prepared for CMIP5 RCP8.5 # cutting out region lon= 0.000 360.000, lat= -90.000 90.000 In fact, I am trying to extract the model type (i.e. bcc-csm1-1) and the forcing scenario (8.5) from line 2. They differ in each folder but they follow the same structure:The model type is between the words "from" and "model" and the forcing scenario is just after "RCP". Does anyone know what code to produce to achieve that? Thank you in advance. -- View this message in context: http://r.789695.n4.nabble.com/How-to-extract-particular-words-from-various-files-tp4099607p4099607.html Sent from the R help mailing list archive at Nabble.com.
How to extract particular words from various files
3 messages · ucakmde, jim holtman
try this: if line locations are not fixed, then use grepl to find the matching line -
x <- readLines(textConnection("# using minimal fraction of valid points 30.00
+ # tas [K] from bcc-csm1-1 model output prepared for CMIP5 RCP8.5 + # cutting out region lon= 0.000 360.000, lat= -90.000 90.000"))
closeAllConnections()
# assume fixed line locations
sub(".*from (.*) model.*", '\\1', x[2])
[1] "bcc-csm1-1"
sub(".*RCP(.*)", "\\1", x[2])
[1] "8.5"
On Wed, Nov 23, 2011 at 8:44 AM, ucakmde <maris_dem at hotmail.com> wrote:
Dear R-help guys, I am trying to extract two particular words from different files but I am struggling with the code. The first theree lines of each folder are as follows: # using minimal fraction of valid points 30.00 # tas [K] from bcc-csm1-1 model output prepared for CMIP5 RCP8.5 # cutting out region lon= ? ?0.000 ?360.000, lat= ?-90.000 ? 90.000 In fact, I am trying to extract the model type (i.e. bcc-csm1-1) and the forcing scenario (8.5) from line 2. They differ in each folder but they follow the same structure:The model type is between the words "from" and "model" and the forcing scenario is just after "RCP". Does anyone know what code to produce to achieve that? Thank you in advance. -- View this message in context: http://r.789695.n4.nabble.com/How-to-extract-particular-words-from-various-files-tp4099607p4099607.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
5 days later
Thank you very much. Your advice solved my problem. -- View this message in context: http://r.789695.n4.nabble.com/How-to-extract-particular-words-from-various-files-tp4099607p4117333.html Sent from the R help mailing list archive at Nabble.com.