I am trying to write a script that stores some pieces of data inside flat .txt files (these are small files, less than 100 lines).
In any case, I try, in fact, to update one suitable line with a new value for this line, leaving everything else in the file, but I canβt figure out how to change only one line, and not replace the complete file.
Here is my code:
# get file contents as array. array_of_lines = File.open( "textfile.txt", "r" ).readlines.map( &:chomp ) line_start = "123456:"
The contents of textfile.txt will always consist of the format:
unique_id: string_of_text
where I can match unique_id with the application data generated by the script to find out which line of text will be updated.
Is there a better way to do what I'm trying to do?
It seems a little inefficient to read the entire file in memory, iterate over everything, only to update one line in that file.
source share