From: Jeremy McAnally Date: 2008-02-18T13:32:03+09:00 Subject: Re: Help with Writing Replacement Text to Files Try this: File.open("config.txt", "rw") do |f| text = f.read text.gsub!(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, "000.000.000.000") f.rewind f.write(text) end Using the block form like that will automagically close the file when it ends. :) --Jeremy On Feb 17, 2008 11:00 PM, Thomas Pierce wrote: > Hello! > > I'm new to ruby and been reading this very active list for about a > couple of week. > > I have a project at work where I need to write a simple script to > scrub IP addresses from configuration files for classified networks we > work in sometimes. The goal is to cleanse configuration files > sometimes required for support of any specific network addressing > information. Here is what I've come up with so far. While this works > in IRB on the file object I create, I cannot figure out how to write > the changes back in place to the actual file itself back on the regex > I'm using: > > config_file = File.open("config.txt") > config_txt = config_file.read > config_txt.gsub!(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, > "000.000.000.000") > > I do understand that the default action here is read-only. How to do I > parse with the regex on the current file and replace matching strings > without either appending to the file, overwriting the file, or > creating a new file? > > Thanks! > > ~Thomas > > -- http://jeremymcanally.com/ http://entp.com Read my books: Ruby in Practice (http://manning.com/mcanally/) My free Ruby e-book (http://humblelittlerubybook.com/) Or, my blogs: http://mrneighborly.com http://rubyinpractice.com