R - Reading lines from a .txt-file after a specific line

后端 未结 3 1032
心在旅途
心在旅途 2020-12-10 16:55

I have a bunch of output .txt-files that consists of a large parameter list and a X-Y-coordinate set. I need to extract these coordinates from all files so that only those l

3条回答
  •  庸人自扰
    2020-12-10 17:25

    An possible approach could be the following:

         conn<-file("file.txt",open="rt")
         x<-TRUE
         while (x)
            {x<-!grepl("coordinatesXY",readLines(conn,n=1))}
         ret<-read.table(conn,...) #insert additional parameters to read.table
         close(conn)
    

    You read one line at the time from the input file and stop when you find the indicator string. Then you read the file through read.table. With this approach you don't store the entire file in memory, but just the piece you need.

提交回复
热议问题