[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Conducting Regular Expressions on a text file

Peter Marks

8/28/2007 12:55:00 AM

I'm trying to find and replacace the string "placeholder" in a text
file. Any idea how I could get the following code to work?

file = File.open('/folder/template.txt', 'r+')
file.gsub!(/placeholder/, "word")

Thanks,

Peter
--
Posted via http://www.ruby-....

8 Answers

Logan Capaldo

8/28/2007 1:53:00 AM

0

On 8/27/07, Peter Marks <petertmarks@gmail.com> wrote:
> I'm trying to find and replacace the string "placeholder" in a text
> file. Any idea how I could get the following code to work?
>
> file = File.open('/folder/template.txt', 'r+')
> file.gsub!(/placeholder/, "word")
>
> Thanks,
>
The most "natural" way would be to use ruby-mmap.
http://raa.ruby-lang.org/pro...

The other method would be of course:

File.open("source.txt", "r") do |src|
File.open("sink.txt", "w") do |sink|
src.each_line { |line| sink.write(line.gsub(/placeholder/, "word")) }
end
end

require 'fileutils'
FileUtils.mv("sink.txt", "source.txt")

This requires twice the space of the file while working so it may not
be viable for large files.

Incidentily ruby has a shortcut for doing this:

ruby -inp -e 'gsub!(/placeholder/, "word")' /folder/template.txt

To handle those really big files where you simply cannot afford to
have two copies simultaneously, or don;t have the address space to map
the whole file into memory with mmap, you can read in fixed sized
chunks, modify them in memory and then write them back out. It's a bit
more complicated than "fixed sized" chunks since the transformed text
may be smaller of larger than the original chunk.
> Peter
> --
> Posted via http://www.ruby-....
>
>

Giles Bowkett

8/28/2007 1:57:00 AM

0

On 8/27/07, Peter Marks <petertmarks@gmail.com> wrote:
> I'm trying to find and replacace the string "placeholder" in a text
> file. Any idea how I could get the following code to work?
>
> file = File.open('/folder/template.txt', 'r+')
> file.gsub!(/placeholder/, "word")
>
> Thanks,
>
> Peter

Bunch of examples here:

http://pleac.sourceforge.net/pleac_ruby/filea...

--
Giles Bowkett

Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles.t...

Peter Marks

8/28/2007 5:14:00 AM

0

Thanks for your thoughful responses Logan and Giles. I'm going to use
the unnatural method for the time being since I'm working with
relatively small files (< 200kb) and I actually need another copy of the
file anyway to preserve the template. However, I'll look into ruby-mapp
when I get more comfortable with all of this (I'm still begining with
ruby).

Thanks again :),

Peter

Giles Bowkett wrote:
> On 8/27/07, Peter Marks <petertmarks@gmail.com> wrote:
>> I'm trying to find and replacace the string "placeholder" in a text
>> file. Any idea how I could get the following code to work?
>>
>> file = File.open('/folder/template.txt', 'r+')
>> file.gsub!(/placeholder/, "word")
>>
>> Thanks,
>>
>> Peter
>
> Bunch of examples here:
>
> http://pleac.sourceforge.net/pleac_ruby/filea...
>
> --
> Giles Bowkett
>
> Blog: http://gilesbowkett.bl...
> Portfolio: http://www.gilesg...
> Tumblelog: http://giles.t...

--
Posted via http://www.ruby-....

Peter Marks

8/28/2007 6:35:00 AM

0

Follow up question:

How might I be able to make multiple placeholders within a text file? I
thought this would work:

File.open("source.txt", "r") do |src|
File.open("sink.txt", "w") do |sink|
src.each_line { |line| sink.write(line.gsub(/placeholder/, "word"))
sink.write(line.gsub(/placeholder2/,
"word2"))
}
end
end

, but my newbie assumptions were mistaken. Any ideas?

Thanks,

Peter
--
Posted via http://www.ruby-....

J-H Johansen

8/28/2007 8:14:00 AM

0

On 8/28/07, Peter Marks <petertmarks@gmail.com> wrote:
> Follow up question:
>
> How might I be able to make multiple placeholders within a text file? I
> thought this would work:
>
> File.open("source.txt", "r") do |src|
> File.open("sink.txt", "w") do |sink|
> src.each_line { |line| sink.write(line.gsub(/placeholder/, "word"))
> sink.write(line.gsub(/placeholder2/,
> "word2"))
> }
> end
> end
>
> , but my newbie assumptions were mistaken. Any ideas?

Nearly there though ;-)
I think you can connect all the gsub's together for that effect.

File.open("source.txt", "r") do |src|
File.open("sink.txt", "w") do |sink|
src.each_line { |line|
sink.write(line.gsub(/placeholder/, "word").gsub(/placeholder2/,"word2"))
}
end
end

You could also create a Hash for placeholder => word in case you have
a lot of things that needs to be changed.

> Thanks,
>
> Peter


--
J-H Johansen
--
There are 10 kinds of people in the world: Those who understand binary and
those who don't...

Peter Marks

8/28/2007 10:10:00 AM

0

J-H Johansen wrote:
> You could also create a Hash for placeholder => word in case you have
> a lot of things that needs to be changed.

Thanks J-H, that works fine. I do have a a lot of these however, so a
hash is probably the be the way to go. This is what I'll be doing:

rehash = {
"placeholder" => "word",
"placeholder2" => "other",
}

File.open('test.txt') do |file|
File.open("sink.rtf", "w") do |sink|
while line = file.gets
sink.write line.gsub(/\[#([^#]*)#\]/){
rehash[$1]
}
end
end
end
--
Posted via http://www.ruby-....

Mark Wilden

8/29/2007 3:28:00 PM

0

On Aug 27, 6:53 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
>
> To handle those really big files where you simply cannot afford to
> have two copies simultaneously, or don;t have the address space to map
> the whole file into memory with mmap, you can read in fixed sized
> chunks, modify them in memory and then write them back out. It's a bit
> more complicated than "fixed sized" chunks since the transformed text
> may be smaller of larger than the original chunk.

The other thing to watch out for is if the search text spans chunks.

///ark

William James

8/29/2007 6:09:00 PM

0

On Aug 27, 8:53 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:

> Incidentily ruby has a shortcut for doing this:
>
> ruby -inp -e 'gsub!(/placeholder/, "word")' /folder/template.txt
>
> To handle those really big files where you simply cannot afford to
> have two copies simultaneously,

I think that Ruby makes a temporary copy of the file.