From: Dave Thomas Date: 2002-05-14T00:45:43+09:00 Subject: Re: Inline editing (non-command line) "Berger, Daniel" writes: > I'm looking for a way to modify a file inline without using a temp file. I > want to do this within code, not on the command line (and no, I'm not > interested in 'system' calls either). I don't believe there's a way to do this on most conventional file systems without sing some temporary storage, either memory f a file. The reason is simple: here's a file on disk: |This is line one\nthis is line two\nand line three| (On DOS the line endings are different, but the principle is the same). So, say you want to change all 't's to 'ZYX's. There's no room in the current file to do it: the first time you change a 't' you'll overwrite the next two characters as well. There are other issues with editing in place: if it blows up half way through, you're left with a half-changed file. So, the conventional way to do this is to copy the original to a new file, then when you're done rename the new file to have the same name as the original. That's what the '-i' option to Ruby does. For example ruby -i -pe 'gsub(/t/, "zyx")' fred replaces all the 't's in the file 'fred' with 'zyx' Regards Dave