A
A
Anton Misyagin2017-02-17 23:07:28
ruby
Anton Misyagin, 2017-02-17 23:07:28

How to read the last line in a text file and then delete it?

There is a large file (250mb) Each line has a link. There is a task that runs on a timer, reads the first / last line, does certain actions with the link, deletes the line from the file, closes the file. How to make it so that it is not resource intensive. Can I have an example?
In principle, it doesn’t matter to me which line is the first or last, the main thing is to quickly deal with the file and close it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2017-02-17
@POS_troi

I don’t remember how this function works in rubles,
but you can get strings

lines = File.open("file.txt").to_a
lines.first
lines.last
lines.at(5) #пять

Well, I'm generally for the use of system utilities - for example
last_line = `tail -n 1 file.txt` # last line

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question