word substitution in file

X

Xan Xann

Hi,

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

For example, if f is:
"
Hello folks or
old men
"

and w = "ol" and x="ur"

then g is:

"
Hello furks or
urd men
"

Can anyone show me some code?
I thinked to pass file to string but I thinked it was not very fast.
Better directly
 
X

Xan Xann

Paul said:
s/File.open(dest)/File.open(dest,"w")/

Also "not tested!". That may seem obvious at this point.

It does not work!!!:
wiki.rb:10: parse error, unexpected kEND
 
E

Eero Saynatkari

Paul said:
#!/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

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'
 
X

Xan Xann

Paul said:
/ ...


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:in `substitute'
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

substitute(f1,f2,"ol","ur")


What happens?
Thanks,
Xan.
 
X

Xan Xann

Eero said:
Paul said:
#!/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

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?

Xan.
 
E

Eero Saynatkari

Xan said:
Eero said:
# 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?

It just substitutes strings with their counterparts
given as the Hash. Regular expression support would
be a bit more involved.
 
X

Xan Xann

Eero said:
Xan said:
Eero said:
# 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?

It just substitutes strings with their counterparts
given as the Hash. Regular expression support would
be a bit more involved.

Well, thanks you very much for your help

Xan.
 
X

Xan Xann

Paul said:
Xan Xann wrote:

/ ...


Here is my example call to 'substitute':

substitute("temp.txt","result.txt","i","x")

You can see that I use strings containing the names of the files.

Here is your example call to 'substitute':

f1 = File.open("original.txt")
f2 = File.open("canviada.txt")

substitute(f1,f2,"ol","ur")

You are using open file handles instead of strings. The remedy is,
instead
of using your example of my example, use my example.

Yes, sorry. What stupid error!!!

Thanks,
Xan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top