Create a test file:
writeLines(c("aaa 11","aaa 22","bbb 33"),con="test.txt")
The brute-force method (essentially the approach you tried above) is to read in the entire thing and then take only the pieces you want:
xx <- readLines("test.txt")
xx <- xx[grepl("aaa",xx,fixed=TRUE)]
## (fixed=TRUE is slightly faster if you don't need regular expressions)
read.table(text=xx)
If you have a big file, I would recommend using grep
at the system level (install Cygwin if necessary as you seem to be using Windows) and using pipe
, e.g.
Test -- see how many lines contain the target string:
system('grep "aaa" <text.txt | wc')
Read only lines containg aaa
:
read.table(pipe('grep "aaa" <test.txt'))
This will be much more efficient than reading the whole thing into R and then selecting the parts you want.