简体   繁体   中英

How do I start reading big file at specific line in ruby

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM