From: Robert Evans Date: 2007-04-05T04:39:04+09:00 Subject: Re: search a file and replace text gsub! is destructive - it edits in-place and does not return a value. You could get a value back from gsub, but then you'd need to do something with that value. The following worked, though I don't consider it elegant: require 'fileutils' File.open("/tmp/replaceable2.txt", 'w+') do | new_file | new_file.puts(File.open('/tmp/replaceable.txt', 'r') do | original_file | original_file.read.gsub('30890', '44444') end) end FileUtils.mv("/tmp/replaceable2.txt", "/tmp/replaceable.txt") If you don't want the intermediate file, you could do something like this: new_str = File.open('/tmp/replaceable.txt', 'r') { | f | f.read.gsub ('30890', '44444') } File.open('/tmp/replaceable.txt', w+) { |f| f.puts new_str } Bob http://www.junitfactory.com/ On Apr 3, 2007, at 10:00 AM, phil.swenson@gmail.com wrote: > well I *THOUGHT* I knew how to do it, here's my first crack at it: > > file_name = base_config_path + '/Caching/tangosol- > coherence.xml' > File.open(file_name, File::RDWR).each{ |line| > if line.gsub!(/30890/, port.to_s) > puts "tangosol port overridden with %d" % port > break > end > } > > > The gsub doesn't modify the line, guess it just modifies the string. > How do I actually modify the file itself? > > btw, I did look at rexml but it looked like overkill for this. I'll > take another look though. > >