From: Jonathan Dobbie Date: 2007-11-10T06:21:30+09:00 Subject: Re: How to edit a file without loading it all into memory 7stud -- wrote: > Jonathan Dobbie wrote: >> The lines are easy to >> find with grep, and I'm able to create the new line easily enough, but >> how can I do this without holding the entire file into memory? The >> files can be hundreds of megabytes. >> > > File.foreach("data.txt") do |line| > #look for something in line > end Here is what I originally wrote, and what I just tried. Am I missing something obvious? The first one duplicates the lines, and the second one does nothing. (doesn't save the changes) def change_texture_paths File.open(Dir.glob(ENV['HOME'] + "/*.ma").first, "r+") do |file| file.grep( /setAttr ".fileTextureName" -type "string"/ ) do |line| texture_file_path = line[52..-2] texture_file_name = texture_file_path.split("\/").last line = " setAttr \".fileTextureName\" -type \"string\" \"" + ENV['HOME'] + "/include/" + texture_file_name + "\n" file.print line end end File.foreach(Dir.glob(ENV['HOME'] + "/*.ma").first, "r+") do |line| if (line =~ /setAttr ".fileTextureName" -type "string"/ ) texture_file_path = line[52..-2] texture_file_name = texture_file_path.split("\/").last line = " setAttr \".fileTextureName\" -type \"string\" \"" + ENV['HOME'] + "/include/" + texture_file_name + "\n" file.print line end end end -- Posted via http://www.ruby-forum.com/.