2

I have a really big file and somehow new data is appended to the end of the file. I only need to read the new data of this file. But now I have to read from the head of this file an scan the whole file.

Is there a good way to start read file from a specific line number? Or is there another way to solve it around?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Chris Kong
  • 389
  • 1
  • 4
  • 18

1 Answers1

0

You can use the tail command from your shell, and use that inside ruby, like this:

lines = `tail -n 10 your_file.txt`

You can also install a gem named elif, which basically works like a regular File module, but allows you to read the file backwards :)

gem install elif

Now, you can do something like this to get the last line from your file:

Elif.open('your_file.txt', &:gets)
Stoic
  • 10,536
  • 6
  • 41
  • 60