I think using the temp file and replacing is a good way. also lets you
revert if something goes wrong. See this Stackoverflow discussion of a
similar question:
http://stackoverflow.com/questions/5452781/edit-each-line-in-a-file-in-ruby

Reading all the info into memory, performing the work, then writing it
out is another way but large files can be an issue

If your worried about performance, i suggest doing some measuring to
see if its really an issue and whether your planned solution is really
better