[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Help with Writing Replacement Text to Files

Thomas Pierce

2/18/2008 4:00:00 AM

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

6 Answers

Jeremy McAnally

2/18/2008 4:32:00 AM

0

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 <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://jeremymca...
http:...

Read my books:
Ruby in Practice (http://manning.com...)
My free Ruby e-book (http://humblelittlerub...)

Or, my blogs:
http://mrneig...
http://rubyinpr...

Andy

2/18/2008 4:38:00 AM

0

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/filea...

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 =3D f.readlines # read into array of lines
lines.each do |it| # modify lines
it.gsub!(/foo/, 'QQQ')
end
f.pos =3D 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.

--=20
-Andy
"I have a great faith in fools; self-confidence my friends call it." =96
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 =3D File.open("config.txt")
> config_txt =3D 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
>
>

7stud --

2/18/2008 6:55:00 AM

0

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)
from r1test.rb:1:in `open'
from r1test.rb:1
--
Posted via http://www.ruby-....

Jeremy McAnally

2/18/2008 7:02:00 AM

0

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)
> from r1test.rb:1:in `open'
> from r1test.rb:1
> --
> Posted via http://www.ruby-....
>
>



--
http://jeremymca...
http:...

Read my books:
Ruby in Practice (http://manning.com...)
My free Ruby e-book (http://humblelittlerub...)

Or, my blogs:
http://mrneig...
http://rubyinpr...

Thomas Pierce

2/18/2008 2:35:00 PM

0

Andy/Jeremy

Thanks for the help!

Jeremy, I ran your code snippet first with "w+" and it wiped the whole =20=

file. I then ran the code snippet Andy provided and it worked. I went =20=

back and used "r+" instead of "w+" in the code you provided Jeremy and =20=

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 =20
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 ..
> http://pleac.sourceforge.net/pleac_ruby/filea...
>
> 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 =3D f.readlines # read into array of lines
> lines.each do |it| # modify lines
> it.gsub!(/foo/, 'QQQ')
> end
> f.pos =3D 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.
>
> --=20
> -Andy
> "I have a great faith in fools; self-confidence my friends call it." =96=

> Edgar Allen Poe
>
> On Feb 17, 2008 9:00 PM, Thomas Pierce <ruby@upgradeyourmind.com> =20
> 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 =20=

>> 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 =3D File.open("config.txt")
>> config_txt =3D 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 =20
>> 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
>>
>>
>


Jeremy McAnally

2/18/2008 3:36:00 PM

0

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 ..
> > http://pleac.sourceforge.net/pleac_ruby/filea...
> >
> > 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 =3D f.readlines # read into array of lines
> > lines.each do |it| # modify lines
> > it.gsub!(/foo/, 'QQQ')
> > end
> > f.pos =3D 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." =
=96
> > 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 =3D File.open("config.txt")
> >> config_txt =3D 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
> >>
> >>
> >
>
>
>



--=20
http://jeremymca...
http:...

Read my books:
Ruby in Practice (http://manning.com...)
My free Ruby e-book (http://humblelittlerub...)

Or, my blogs:
http://mrneig...
http://rubyinpr...