Can anyone help me in that?
I just want to write the following function:
substitute(f,g,w,x)
where f and g are files and w and x are strings
That function substitutes all the ocurrences of w in the file f and
writes x to g
substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,#{replace})
File.open(dest) { |f| f.write(result) } if data != result
end
I thinked to pass file to string but I thinked it was not very fast.
Better directly
Not necessarily. Putting a file’s contents into a string is the best way
to
carry out substitutions, especially when regular expressions are
involved.
If the files are large, one can write a line-by-line, therefore
streaming,
solution, from one file to another, but this also has limitations when
sophisticated regular expressions are required.
Also “not tested!”. That may seem obvious at this point.
It does not work!!!:
wiki.rb:10: parse error, unexpected kEND
My original code was actually pseudo-code, unbeknownst to me.
#!/usr/bin/ruby
def substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,replace)
File.open(dest,“w”) { |f| f.write(result) } if data != result
end
substitute(“temp.txt”,“result.txt”,“i”,“x”)
temp.txt = “This is a test line.”
result.txt = “Thxs xs a test lxne.”
Tested, which I should have done in the first place.
Also “not tested!”. That may seem obvious at this point.
It does not work!!!:
wiki.rb:10: parse error, unexpected kEND
My original code was actually pseudo-code, unbeknownst to me.
#!/usr/bin/ruby
def substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,replace)
File.open(dest,“w”) { |f| f.write(result) } if data != result
end
substitute(“temp.txt”,“result.txt”,“i”,“x”)
temp.txt = “This is a test line.”
result.txt = “Thxs xs a test lxne.”
Tested, which I should have done in the first place.
I get this error:
wiki.rb:7:in read': can't convert File into String (TypeError) from wiki.rb:7:insubstitute’
from wiki.rb:12
with code:
require ‘fileutils’
f1 = File.open("original.txt")
f2 = File.open("canviada.txt")
def substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,replace)
File.open(dest,“w”) { |f| f.write(result) } if data != result
end
def substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,replace)
File.open(dest,“w”) { |f| f.write(result) } if data != result
end
The equality test there is probably unnecessary.
What you could do for a bit lower a cost, if you
really want to avoid the possible write overhead:
Untested too
require ‘fileutils’
def FileUtils.s(source, dest, subs = {})
data = File.read source
search = /#{subs.keys.map {|k| Regexp.escape k}.join ‘|’}/
result = data.gsub(search) {|found| subs[found]}
File.open(dest, ‘w’) {|f| f.write result} if $& # Regex matched
end
def substitute(source,dest,find,replace)
data = File.read(source)
result = data.gsub(/#{find}/,replace)
File.open(dest,“w”) { |f| f.write(result) } if data != result
end
The equality test there is probably unnecessary.
What you could do for a bit lower a cost, if you
really want to avoid the possible write overhead:
Untested too
require ‘fileutils’
def FileUtils.s(source, dest, subs = {})
data = File.read source
search = /#{subs.keys.map {|k| Regexp.escape k}.join ‘|’}/
result = data.gsub(search) {|found| subs[found]}
File.open(dest, ‘w’) {|f| f.write result} if $& # Regex matched
end
FileUtils.s ‘src.txt’, ‘dest.txt’, ‘foo’ => ‘bar’
This WORKS!!! finally
Thank you very much
It seems it supports regexp expressions. Is it true?