Mauro
Mauro

Reputation: 107

Can I skip until I reach a certain line when iterating through text in RStudio?

I am reading text data using the read.delim() -&- read.delim2() methods. They accept a skip argument, but it works with number of lines, (i.e. it skips 2,3,4,100 the line you pass into it).

I am using the methods...

read.delim()

read.delim2()

...to read text data. The methods above are able to skip lines, consequently; the methods have a skip parameter, and the parameter accepts an array of line-numbers as an argument. All line-numbers passed into the skip-parameter are skipped by the reader methods (i.e. the lines are not read by the reader methods).

I want to iterate through a body of text, skipping every line until I get to a specific line. Does anyone know how this can be done?

Upvotes: 0

Views: 724

Answers (1)

r2evans
r2evans

Reputation: 160687

You cannot do that in base R functions, and I don't know of a package that directly provides that. However, here are two ways to get the effect.

First, a file named file.txt:

I want to skip this
and this too
Absolute Irradiance
I need this line
txt <- readLines("file.txt")
txt[cumany(grepl("Absolute Irradiance", txt))]
# [1] "Absolute Irradiance" "I need this line"   

If you don't want the "Irradiance" line but want everything after it, then add [-1] to remove the first of the returned lines:

txt[cumany(grepl("Absolute Irradiance", txt))][-1]
# [1] "I need this line"

If the file is relatively large and you do not want to read all of it into R, then

system2("sed", c("-ne", "'/Absolute Irradiance/,$p'", "file.txt"), stdout = TRUE)
# [1] "Absolute Irradiance" "I need this line"   

This second technique is really not that great ... it might be better to run that from file.txt into a second (temp) file, then just readLines("tempfile.txt") directly.

Upvotes: 3

Related Questions