# Problem with gsub! in file

**URL:** <https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735>\
**Category:** Ruby\
**Created:** [March 24, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735 "2009-03-24T14:35:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 2:35pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/1 "2009-03-24T14:35:31Z")

</div>

I am new to Ruby and trying to do a find and replace on a text file  
based on a regular expression. When I step through the code line by  
line, it appears that the appropriate line in the file is changed, but  
when I exit the function, the file hasn’t been modified. Are in-place  
edits of files allowed? can you see what it wrong here?

Here is the body of the function:

def ReplaceGuid(guid)  
r = nil  
sconfProj = File.open(“myfile.vcproj”, “r+”).each do |line|  
m = @guidExp.match(line)  
if !m.nil?  
str = m[1]  
r = %r{#{str}}  
line.gsub!(r, guid)  
break  
end  
end  
end

Many thanks,  
Jen

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 4:00pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/2 "2009-03-24T16:00:53Z")

</div>

OK, I see now it’s not trivial to read and write from a file at the  
same time, and instead I should probably just drop to the command line  
and call  
`ruby -pe 'gsub(/foo/, "bar")' < myfile.vcproj`

When i do this, the text of the file spits out to stdio, and it  
appears the substitution has been made, but when I open the file in a  
text editor, it hasn’t been changed.  
Am I missing something?  
I already checked file permissions and everything looks fine

Thanks,  
Jen

`

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/3 "2009-03-24T16:17:27Z")

</div>

JenC wrote:

> OK, I see now it’s not trivial to read and write from a file at the  
> same time, and instead I should probably just drop to the command line  
> and call  
> `ruby -pe 'gsub(/foo/, "bar")' < myfile.vcproj`
> 
> When i do this, the text of the file spits out to stdio, and it  
> appears the substitution has been made, but when I open the file in a  
> text editor, it hasn’t been changed.  
> Am I missing something?  
> I already checked file permissions and everything looks fine
> 
> Thanks,  
> Jen
> 
> `

You’ll want ruby -pie ‘gsub(/foo/,“bar”)’ myfile.vcproj

Or, if you want to keep a backup of the original (and you probably do,  
until you know this works the way you expect), you can add an extension  
of your choice to the -i flag:

ruby -p -i.bak e ‘gsub(/foo/,“bar”)’ myfile.vcproj

I’ve actually never used this on Windows, you’ll have to try it and see.

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 4:19pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/4 "2009-03-24T16:19:16Z")

</div>

Adam G. wrote:

> ruby -p -i.bak e ‘gsub(/foo/,“bar”)’ myfile.vcproj

Er, sorry, make that ruby -p -i.bak -e ‘gsub(/foo/,“bar”)’  
myfile.vcproj

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/5 "2009-03-24T17:31:57Z")

</div>

Thanks Adam. I tried this:  
ruby -p -i.bak -e ‘gsub(/foo/,“bar”)’ myfile.vcproj

but them I get a whole load of warnings:  
-e:1: warning: Can’t do inplace edit for stdio

and the file still doesn’t change… any ideas?

best,  
Jen

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 24, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/6 "2009-03-24T17:49:28Z")

</div>

JenC wrote:

> Thanks Adam. I tried this:  
> ruby -p -i.bak -e ‘gsub(/foo/,“bar”)’ myfile.vcproj
> 
> but them I get a whole load of warnings:  
> -e:1: warning: Can’t do inplace edit for stdio
> 
> and the file still doesn’t change… any ideas?
> 
> best,  
> Jen

Are you sure you did just ‘myfile.vcproj’ and not ‘\< myfile.vcproj’?

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 25, 2009, 2:28pm UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/7 "2009-03-25T14:28:12Z")

</div>

> > ```
> > sconfProj = File.open("myfile.vcproj", "r+").each do |line|
> > 
> > ```
> > 
> > Many thanks,  
> > Jen

The problem is this: you’re reading the contents of your file into  
memory, changing the copy in memory and expecting it to propagate to the  
physical file. Try this: (though I haven’t tested or run it, so there  
might be some errors)

def ReplaceGuid(guid)  
outfile = File.new(“Some\_filename”, ‘w’)  
found\_match = false  
IO.read(“myfile.vcproj”).each\_line do |line|  
if (@guidExp =~ (line)) && !found\_match  
line.gsub!($1, guid)  
found\_match = true  
end  
outfile.puts line  
end  
outfile.close  
end

That reads in the original and writes the output to a new file. I’m  
assuming you wanted to stop at the first match, which is what would  
happen in your original code.

HTH,  
Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![JenC](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/71c47a/32.png) [@JenC](https://www.ruby-forum.com/u/JenC)\
**Post date:** [March 25, 2009, 9:56am UTC](https://www.ruby-forum.com/t/problem-with-gsub-in-file/163735/8 "2009-03-25T09:56:03Z")

</div>

> Are you sure you did just ‘myfile.vcproj’ and not ‘\< myfile.vcproj’?  
> aha! that was it!

Thanks so much!

On Mar 24, 5:46 pm, Adam G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Jen
