# Finding and writing to specified area in a text file

**URL:** <https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326>\
**Category:** Ruby\
**Created:** [November 14, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326 "2008-11-14T22:12:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [November 14, 2008, 10:12pm UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326/1 "2008-11-14T22:12:34Z")

</div>

Hello,

I’m trying to find a specified keyword in a text file that I have open  
then replace the line of that keyword with some new text. Is it  
possible do do this?

So something like:

#custom.txt  
hello world  
INSERT  
end of hello world

So INSERT would be swapped with some new text.

Thanks

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [November 15, 2008, 12:10am UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326/2 "2008-11-15T00:10:40Z")

</div>

In all of my research online, the only way that I’ve found to do it is  
to read the entire file, replace what you wanna replace, write to a new  
file, then copy over the original one. I hope someone can show me a  
better way.

–Aldric

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [November 15, 2008, 12:18am UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326/3 "2008-11-15T00:18:32Z")

</div>

Aldric G. wrote:

> In all of my research online, the only way that I’ve found to do it is  
> to read the entire file, replace what you wanna replace, write to a new  
> file, then copy over the original one. I hope someone can show me a  
> better way.
> 
> –Aldric

Hi Aldric,

I’m actually looking to replace some text/code in 3-4 places so i can’t  
hardcode specific lines into the model because they could change. All  
of this needs to be done on a button click so the actual file is hidden  
from the user. I’m eventually wanting to add form input boxes and use  
them to replace the text/code areas.

Thanks for your help. Please could you put up some of your code for me  
to study?

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [November 20, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326/4 "2008-11-20T17:55:42Z")

</div>

Aldric G. wrote:

> In all of my research online, the only way that I’ve found to do it is  
> to read the entire file, replace what you wanna replace, write to a new  
> file, then copy over the original one. I hope someone can show me a  
> better way.
> 
> –Aldric

You know , there is an alternative to writing to a new file . You will  
have to read the file contents , modify them , seek to the file’s start  
, write the modified contents and truncate at the current position .

---

<div class="post-metadata">

**Author:** ![rmac217](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bc8723/32.png) [@rmac217](https://www.ruby-forum.com/u/rmac217)\
**Post date:** [November 20, 2008, 5:51pm UTC](https://www.ruby-forum.com/t/finding-and-writing-to-specified-area-in-a-text-file/153326/5 "2008-11-20T17:51:23Z")

</div>

A gsub (global substitution) should take care of the problem. Here’s an  
example of how I might do this:

######################################################

data = “”

File.open(“my\_file.txt”, “r”) do |file|  
data = file.read.gsub(“INSERT”, “new word”)  
end

File.open(“my\_file.txt”, “w”) do |file|  
data = file.write(data)  
end

######################################################

The gsub command also supports regular expressions, if you need it.
