Weird result of replacing content

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

a_rep.txt

http.log

http_rep.log

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs