# Weird result of replacing content

**URL:** <https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775>\
**Category:** Ruby\
**Created:** [August 1, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775 "2008-08-01T23:05:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [August 1, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775/1 "2008-08-01T23:05:23Z")

</div>

Hi, there

I’m stuck on replacing patterns in a file.

I have http.log, which is generated from system; and a.txt, which is  
manually written; Also, i have a replace function that takes string and  
replace a pattern.

code is as follow:

def replace(filename,pattern, line)  
content=Array.new()  
File.open(filename, ‘r+’) do |file|  
#while repcontent=file.gets()  
content = file.read.gsub(/#{pattern}/, line)  
file.seek(0)  
file.write(content)  
#end  
end

end

replace(‘logs/http.log’,"(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/20\d\d",“dd/mm/yy”)

#replace(‘a.txt’,“bb”,“aa”)

The attachments are  
the original file http.log and it’s replaced file http\_rep.log

The replace function works fine for a.txt, but it gives extra line in  
http\_rep.log

Is there anyone know what’s going on here?

the original file a.txt and it’s replaced file a\_replaced.txt

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [August 1, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775/2 "2008-08-01T23:05:55Z")

</div>

a\_rep.txt

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [August 1, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775/3 "2008-08-01T23:08:31Z")

</div>

http.log

---

<div class="post-metadata">

**Author:** ![cheyne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b9bd4f/32.png) [@cheyne](https://www.ruby-forum.com/u/cheyne)\
**Post date:** [August 1, 2008, 11:08pm UTC](https://www.ruby-forum.com/t/weird-result-of-replacing-content/144775/4 "2008-08-01T23:08:59Z")

</div>

http\_rep.log
