# Search and replace text in files

**URL:** <https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282>\
**Category:** Ruby\
**Created:** [May 3, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282 "2007-05-03T19:21:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [May 3, 2007, 7:21pm UTC](https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282/1 "2007-05-03T19:21:13Z")

</div>

I need to search a file for text “InstallDir=” + newline character and  
replace it with something like: InstallDir=C:\mws71 + newline

My own experimentation with this had problems with writing the back-  
slashes to the file (it’s a requirement of the format to have the back-  
slashes as in my example). The string had the back-slashes in it but  
they didn’t get persisted to the file correctly.

Are there any libraries that perform a search and replace?

thanks  
phil

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [May 3, 2007, 8:16pm UTC](https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282/2 "2007-05-03T20:16:35Z")

</div>

Phil S. wrote:

> I need to search a file for text “InstallDir=” + newline character and  
> replace it with something like: InstallDir=C:\mws71 + newline

> > “InstallDir=\n”.gsub /(InstallDir=)(\n)/, ‘\1C:\mws71\2’  
> > =\> “InstallDir=C\:\mws71\n”

> My own experimentation with this had problems with writing the back-  
> slashes to the file (it’s a requirement of the format to have the back-  
> slashes as in my example). The string had the back-slashes in it but  
> they didn’t get persisted to the file correctly.

In a single-quoted string, one backslash outputs one backslash.  
In a double-quoted string, two backslashes outputs one backslash.

> > '\ ’  
> > =\> "\ "

> > "\ "  
> > =\> "\ "

> Are there any libraries that perform a search and replace?

String#gsub

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [May 3, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282/3 "2007-05-03T21:04:59Z")

</div>

On Fri, May 04, 2007 at 03:16:37AM +0900, Suraj K. wrote:

> > they didn’t get persisted to the file correctly.
> 
> In a single-quoted string, one backslash outputs one backslash.

Except where followed by a single quote or another backslash.

irb(main):001:0\> ‘\’.length  
=\> 1  
irb(main):002:0\> ‘’’.length  
=\> 1  
irb(main):003:0\> ‘\n’.length  
=\> 2

---

<div class="post-metadata">

**Author:** ![philswenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7feea3/32.png) [@philswenson](https://www.ruby-forum.com/u/philswenson)\
**Post date:** [May 3, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/search-and-replace-text-in-files/103282/4 "2007-05-03T21:36:02Z")

</div>

> > Are there any libraries that perform a search and replace?
> 
> String#gsub

I meant on a file… I was looking for something like  
File.search\_and\_replace(file\_name, regex\_search, replace\_string)

But I guess I can simply pull a file into a string and then do a gsub  
and write the file out.

Thanks,  
phil
