# Conducting Regular Expressions on a text file

**URL:** <https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143>\
**Category:** Ruby\
**Created:** [August 28, 2007, 2:55am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143 "2007-08-28T02:55:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 2:55am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/1 "2007-08-28T02:55:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 3:53am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/2 "2007-08-28T03:53:50Z")

</div>

On 8/27/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/project/mmap/](http://raa.ruby-lang.org/project/mmap/)

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.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 3:57am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/3 "2007-08-28T03:57:51Z")

</div>

On 8/27/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/fileaccess.html](http://pleac.sourceforge.net/pleac_ruby/fileaccess.html)

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 8:34am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/4 "2007-08-28T08:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 7:14am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/5 "2007-08-28T07:14:04Z")

</div>

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 B. wrote:

> On 8/27/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:
> 
> [File Access](http://pleac.sourceforge.net/pleac_ruby/fileaccess.html)
> 
> –  
> Giles B.
> 
> Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
> Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
> Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 10:15am UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/6 "2007-08-28T10:15:31Z")

</div>

On 8/28/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> }  
> 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.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 28, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/7 "2007-08-28T12:10:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 29, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/8 "2007-08-29T20:11:34Z")

</div>

On Aug 27, 8:53 pm, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [August 29, 2007, 5:31pm UTC](https://www.ruby-forum.com/t/conducting-regular-expressions-on-a-text-file/113143/9 "2007-08-29T17:31:02Z")

</div>

On Aug 27, 6:53 pm, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
