2

Possible Duplicate:
Reading the last n lines from a huge text file

I have created a connection to a file using

con=file(path_of_myfile)

Now I want to read only the last line without loading everything (it is a HUGE file).

I am trying to use

?readLines

with no success.

Any idea?

Community
  • 1
  • 1
RockScience
  • 17,932
  • 26
  • 89
  • 125
  • does R have anything like a *seek* function? – jcomeau_ictx Jun 07 '11 at 11:02
  • This question is not an exact duplicate of http://stackoverflow.com/questions/5596107/reading-the-last-n-lines-from-a-huge-text-file since the restriction to `n=1` here gives rise to potentially simpler solutions. This is due to the fact that you typically have the last line available when you hit the end of the file. – G. Grothendieck Jun 10 '11 at 02:24
  • @RockScience Have you tried `count.felds()`? Try `length(count.fields("foo.txt"))`. It should be platform independent, but it will probably be slower since it _counts fields_. – Vulpecula Aug 05 '11 at 21:10

2 Answers2

8

Since you are on Windows, download and install Duncan's Rtools which you would need anyways if you wanted to build R packages yourself. (If you were on Linux then the only difference is that you would not need to download anything since gawk is already there.) Then issue this R command:

system("gawk 'END {print}' myfile", intern = TRUE)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • I have an Error in system... : 'gawk' not found Do I have to add something to the PATH? – RockScience Jun 08 '11 at 03:28
  • @Rockcience, Assuming Rtools is in `C:\Rtools`, add `C:\Rtools\bin` to your path. Alternately, use `Rgui.bat` from `http://batchfiles.googlecode.com` to start R (in place of `Rgui.exe`) in which case it will automatically add the required directory to your path for the duration of the R session without you having to change anything. – G. Grothendieck Jun 08 '11 at 10:44
5

If you're running on a unix-like system you could possibly use wc to count lines and use scan() in R with the skip argument:

lastline <- function(filename) {
  ## filename is of mode character
  out <- system(sprintf("wc -l %s",filename),intern=TRUE)
  n <- as.integer(sub(sprintf("[ ]*([0-9]+)[ ]%s",filename),"\\1",out))
  print(n)
  scan(filename,what="",skip=n-1,nlines=1,sep="\n",quiet=TRUE)
}

> lastline("myfile")
hatmatrix
  • 42,883
  • 45
  • 137
  • 231