# Help with Writing Replacement Text to Files

**URL:** <https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577>\
**Category:** ruby-talk\
**Created:** [18 February 2008 04:00 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577 "2008-02-18T04:00:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas\_Pierce](https://avatars.discourse-cdn.com/v4/letter/t/7cd45c/32.png) [@Thomas\_Pierce](https://rubytalk.org/u/Thomas_Pierce)\
**Post date:** [18 February 2008 04:00 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/1 "2008-02-18T04:00:29Z")

</div>

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:

&nbsp;&nbsp;config\_file = File.open("config.txt")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_txt = config\_file.read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally1](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jeremy\_McAnally1](https://rubytalk.org/u/Jeremy_McAnally1)\
**Post date:** [18 February 2008 04:32 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/2 "2008-02-18T04:32:03Z")

</div>

Try this:

File.open("config.txt", "rw") do |f|  
&nbsp;&nbsp;text = f.read  
&nbsp;&nbsp;text.gsub!(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, "000.000.000.000")  
&nbsp;&nbsp;f.rewind  
&nbsp;&nbsp;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 \<ruby@upgradeyourmind.com\> 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:
> > 
> > &nbsp;&nbsp;config\_file = File.open("config.txt")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_txt = config\_file.read  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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://jeremymcanally.com/)  
> [http://entp.com](http://entp.com)
> 
> Read my books:  
> Ruby in Practice ([http://manning.com/mcanally/\](http://manning.com/mcanally/%5C))  
> My free Ruby e-book ([http://humblelittlerubybook.com/\](http://humblelittlerubybook.com/%5C))
> 
> Or, my blogs:
> 
> > **[Site not found · DreamHost](http://mrneighborly.com)**
> >
> > The owner of this domain has not yet uploaded their website.
> 
> [http://rubyinpractice.com](http://rubyinpractice.com)

---

<div class="post-metadata">

**Author:** ![Andy](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Andy](https://rubytalk.org/u/Andy)\
**Post date:** [18 February 2008 04:38 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/3 "2008-02-18T04:38:11Z")

</div>

Thomas,

I'm new to Ruby (only a few days) but this is something I think I can  
actually help you with. I had to do some searching and I came across  
this website ..  
[http://pleac.sourceforge.net/pleac\_ruby/fileaccess.html](http://pleac.sourceforge.net/pleac_ruby/fileaccess.html)

Search for 'Modifying a File in Place Without a Temporary File' and  
you'll find the code. I'll paste it here though just to make it quick  
and easy ..

File.open('itest', 'r+') do |f| # open file for update  
&nbsp;&nbsp;&nbsp;&nbsp;lines = f.readlines # read into array of lines  
&nbsp;&nbsp;&nbsp;&nbsp;lines.each do |it| # modify lines  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it.gsub!(/foo/, 'QQQ')  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;f.pos = 0 # back to start  
&nbsp;&nbsp;&nbsp;&nbsp;f.print lines # write out modified lines  
&nbsp;&nbsp;&nbsp;&nbsp;f.truncate(f.pos) # truncate to new length  
end # file is automatically closed

r+ is the mode that allows you to read and write to the file. I made  
a text file named itest with this for content ..

foo foo  
bar bar  
foo foo

I ran that code and the file got changed to ..

QQQ QQQ  
bar bar  
QQQ QQQ

That's what you're looking for isn't it? I would suggest making a  
backup of the files just in case there is a problem. If that isn't  
what you were looking for I apologize, reply back to this and I'll try  
again haha.

> **···**
>
> --  
> -Andy  
> "I have a great faith in fools; self-confidence my friends call it." –  
> Edgar Allen Poe
> 
> On Feb 17, 2008 9:00 PM, Thomas Pierce \<ruby@upgradeyourmind.com\> 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:
> > 
> > &nbsp;&nbsp;config\_file = File.open("config.txt")  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_txt = config\_file.read  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [18 February 2008 06:55 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/4 "2008-02-18T06:55:19Z")

</div>

Jeremy McAnally wrote:

> Try this:
> 
> File.open("config.txt", "rw") do |f|

File.open('data.text', 'rw') do |f|  
end

--output:--  
r1test.rb:1:in `initialize': illegal access mode rw (ArgumentError)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:1:in `open'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:1

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Thomas\_Pierce](https://avatars.discourse-cdn.com/v4/letter/t/7cd45c/32.png) [@Thomas\_Pierce](https://rubytalk.org/u/Thomas_Pierce)\
**Post date:** [18 February 2008 14:35 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/5 "2008-02-18T14:35:26Z")

</div>

Andy/Jeremy

Thanks for the help!

Jeremy, I ran your code snippet first with "w+" and it wiped the whole file. I then ran the code snippet Andy provided and it worked. I went back and used "r+" instead of "w+" in the code you provided Jeremy and it worked as well.

I also appreciate the explanatory comments. Very helpful.

Both code snippets work and provide me with exactly what I need.

Thanks again.

Hopefully as I move along I'll be able to be just as helpful on this list.

Best Regards,

~Thomas

> **···**
>
> On Feb 17, 2008, at 11:38 PM, Andy wrote:
> 
> > Thomas,
> > 
> > I'm new to Ruby (only a few days) but this is something I think I can  
> > actually help you with. I had to do some searching and I came across  
> > this website ..  
> > [File Access](http://pleac.sourceforge.net/pleac_ruby/fileaccess.html)
> > 
> > Search for 'Modifying a File in Place Without a Temporary File' and  
> > you'll find the code. I'll paste it here though just to make it quick  
> > and easy ..
> > 
> > File.open('itest', 'r+') do |f| # open file for update  
> > &nbsp;&nbsp;&nbsp;lines = f.readlines # read into array of lines  
> > &nbsp;&nbsp;&nbsp;lines.each do |it| # modify lines  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;it.gsub!(/foo/, 'QQQ')  
> > &nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;f.pos = 0 # back to start  
> > &nbsp;&nbsp;&nbsp;f.print lines # write out modified lines  
> > &nbsp;&nbsp;&nbsp;f.truncate(f.pos) # truncate to new length  
> > end # file is automatically closed
> > 
> > r+ is the mode that allows you to read and write to the file. I made  
> > a text file named itest with this for content ..
> > 
> > foo foo  
> > bar bar  
> > foo foo
> > 
> > I ran that code and the file got changed to ..
> > 
> > QQQ QQQ  
> > bar bar  
> > QQQ QQQ
> > 
> > That's what you're looking for isn't it? I would suggest making a  
> > backup of the files just in case there is a problem. If that isn't  
> > what you were looking for I apologize, reply back to this and I'll try  
> > again haha.
> > 
> > --  
> > -Andy  
> > "I have a great faith in fools; self-confidence my friends call it." –  
> > Edgar Allen Poe
> > 
> > On Feb 17, 2008 9:00 PM, Thomas Pierce \<ruby@upgradeyourmind.com\> \> 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")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;config\_txt = config\_file.read  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally1](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jeremy\_McAnally1](https://rubytalk.org/u/Jeremy_McAnally1)\
**Post date:** [18 February 2008 07:01 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/6 "2008-02-18T07:01:55Z")

</div>

Oops. Try "w+" instead.

\*has Python/PHP file read modes on the brain\*

--Jeremy

> **···**
>
> On Feb 18, 2008 1:55 AM, 7stud -- \<bbxx789\_05ss@yahoo.com\> wrote:
> 
> > Jeremy McAnally wrote:  
> > \> Try this:  
> > \>  
> > \> File.open("config.txt", "rw") do |f|  
> > \>
> > 
> > File.open('data.text', 'rw') do |f|  
> > end
> > 
> > --output:--  
> > r1test.rb:1:in `initialize': illegal access mode rw (ArgumentError)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:1:in `open'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from r1test.rb:1  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> [http://jeremymcanally.com/](http://jeremymcanally.com/)  
> [http://entp.com](http://entp.com)
> 
> Read my books:  
> Ruby in Practice ([http://manning.com/mcanally/\](http://manning.com/mcanally/%5C))  
> My free Ruby e-book ([http://humblelittlerubybook.com/\](http://humblelittlerubybook.com/%5C))
> 
> Or, my blogs:
> 
> > **[Site not found · DreamHost](http://mrneighborly.com)**
> >
> > The owner of this domain has not yet uploaded their website.
> 
> [http://rubyinpractice.com](http://rubyinpractice.com)

---

<div class="post-metadata">

**Author:** ![Jeremy\_McAnally1](https://avatars.discourse-cdn.com/v4/letter/j/eb8c5e/32.png) [@Jeremy\_McAnally1](https://rubytalk.org/u/Jeremy_McAnally1)\
**Post date:** [18 February 2008 15:36 UTC](https://rubytalk.org/t/help-with-writing-replacement-text-to-files/44577/7 "2008-02-18T15:36:21Z")

</div>

Oh, right. I forget that the "\*+" modes do the primary letter ("w"  
wipes the file for example) first, then "add on" the functionality of  
the other (this must show how much I actually use those modes, eh?  
:)). Well, glad to help nonetheless.

--Jeremy

> **···**
>
> On Feb 18, 2008 9:35 AM, Thomas Pierce \<ruby@upgradeyourmind.com\> wrote:
> 
> > Andy/Jeremy
> > 
> > Thanks for the help!
> > 
> > Jeremy, I ran your code snippet first with "w+" and it wiped the whole  
> > file. I then ran the code snippet Andy provided and it worked. I went  
> > back and used "r+" instead of "w+" in the code you provided Jeremy and  
> > it worked as well.
> > 
> > I also appreciate the explanatory comments. Very helpful.
> > 
> > Both code snippets work and provide me with exactly what I need.
> > 
> > Thanks again.
> > 
> > Hopefully as I move along I'll be able to be just as helpful on this  
> > list.
> > 
> > Best Regards,
> > 
> > ~Thomas
> > 
> > On Feb 17, 2008, at 11:38 PM, Andy wrote:
> > 
> > \> Thomas,  
> > \>  
> > \> I'm new to Ruby (only a few days) but this is something I think I can  
> > \> actually help you with. I had to do some searching and I came across  
> > \> this website ..  
> > \> [File Access](http://pleac.sourceforge.net/pleac_ruby/fileaccess.html)  
> > \>  
> > \> Search for 'Modifying a File in Place Without a Temporary File' and  
> > \> you'll find the code. I'll paste it here though just to make it quick  
> > \> and easy ..  
> > \>  
> > \> File.open('itest', 'r+') do |f| # open file for update  
> > \> lines = f.readlines # read into array of lines  
> > \> lines.each do |it| # modify lines  
> > \> it.gsub!(/foo/, 'QQQ')  
> > \> end  
> > \> f.pos = 0 # back to start  
> > \> f.print lines # write out modified lines  
> > \> f.truncate(f.pos) # truncate to new length  
> > \> end # file is automatically closed  
> > \>  
> > \> r+ is the mode that allows you to read and write to the file. I made  
> > \> a text file named itest with this for content ..  
> > \>  
> > \> foo foo  
> > \> bar bar  
> > \> foo foo  
> > \>  
> > \> I ran that code and the file got changed to ..  
> > \>  
> > \> QQQ QQQ  
> > \> bar bar  
> > \> QQQ QQQ  
> > \>  
> > \> That's what you're looking for isn't it? I would suggest making a  
> > \> backup of the files just in case there is a problem. If that isn't  
> > \> what you were looking for I apologize, reply back to this and I'll try  
> > \> again haha.  
> > \>  
> > \> --  
> > \> -Andy  
> > \> "I have a great faith in fools; self-confidence my friends call it." –  
> > \> Edgar Allen Poe  
> > \>  
> > \> On Feb 17, 2008 9:00 PM, Thomas Pierce \<ruby@upgradeyourmind.com\> \> \> 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://jeremymcanally.com/)  
> [http://entp.com](http://entp.com)
> 
> Read my books:  
> Ruby in Practice ([http://manning.com/mcanally/\](http://manning.com/mcanally/%5C))  
> My free Ruby e-book ([http://humblelittlerubybook.com/\](http://humblelittlerubybook.com/%5C))
> 
> Or, my blogs:
> 
> > **[Site not found · DreamHost](http://mrneighborly.com)**
> >
> > The owner of this domain has not yet uploaded their website.
> 
> [http://rubyinpractice.com](http://rubyinpractice.com)
