Read, edit, and write a text file line-wise using Ruby Read, edit, and write a text file line-wise using Ruby ruby ruby

Read, edit, and write a text file line-wise using Ruby


In general, there's no way to make arbitrary edits in the middle of a file. It's not a deficiency of Ruby. It's a limitation of the file system: Most file systems make it easy and efficient to grow or shrink the file at the end, but not at the beginning or in the middle. So you won't be able to rewrite a line in place unless its size stays the same.

There are two general models for modifying a bunch of lines. If the file is not too large, just read it all into memory, modify it, and write it back out. For example, adding "Kilroy was here" to the beginning of every line of a file:

path = '/tmp/foo'lines = IO.readlines(path).map do |line|  'Kilroy was here ' + lineendFile.open(path, 'w') do |file|  file.puts linesend

Although simple, this technique has a danger: If the program is interrupted while writing the file, you'll lose part or all of it. It also needs to use memory to hold the entire file. If either of these is a concern, then you may prefer the next technique.

You can, as you note, write to a temporary file. When done, rename the temporary file so that it replaces the input file:

require 'tempfile'require 'fileutils'path = '/tmp/foo'temp_file = Tempfile.new('foo')begin  File.open(path, 'r') do |file|    file.each_line do |line|      temp_file.puts 'Kilroy was here ' + line    end  end  temp_file.close  FileUtils.mv(temp_file.path, path)ensure  temp_file.close  temp_file.unlinkend

Since the rename (FileUtils.mv) is atomic, the rewritten input file will pop into existence all at once. If the program is interrupted, either the file will have been rewritten, or it will not. There's no possibility of it being partially rewritten.

The ensure clause is not strictly necessary: The file will be deleted when the Tempfile instance is garbage collected. However, that could take a while. The ensure block makes sure that the tempfile gets cleaned up right away, without having to wait for it to be garbage collected.


If you want to overwrite a file line by line, you'll have to ensure the new line has the same length as the original line. If the new line is longer, part of it will be written over the next line. If the new line is shorter, the remainder of the old line just stays where it is.The tempfile solution is really much safer. But if you're willing to take a risk:

File.open('test.txt', 'r+') do |f|       old_pos = 0    f.each do |line|        f.pos = old_pos   # this is the 'rewind'        f.print line.gsub('2010', '2011')        old_pos = f.pos    endend

If the line size does change, this is a possibility:

File.open('test.txt', 'r+') do |f|       out = ""    f.each do |line|        out << line.gsub(/myregex/, 'blah')     end    f.pos = 0                         f.print out    f.truncate(f.pos)             end


Just in case you are using Rails or Facets, or you otherwise depend on Rails' ActiveSupport, you can use the atomic_write extension to File:

File.atomic_write('path/file') do |file|  file.write('your content')end

Behind the scenes, this will create a temporary file which it will later move to the desired path, taking care of closing the file for you.

It further clones the file permissions of the existing file or, if there isn't one, of the current directory.