Word substitution in file

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

···

--
Posted via http://www.ruby-forum.com/.

def substitute(f,g,w,x) `sed s/#{w}/#{x}/g < #{f} > #{g}` end

Xan Xann wrote:

···

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

Xan Xann wrote:

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

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.

···

--
Paul Lutus
http://www.arachnoid.com

Jason Nordwick wrote:

def substitute(f,g,w,x) `sed s/#{w}/#{x}/g < #{f} > #{g}` end

Thank you but I get this errors:

sh: -c: line 0: syntax error near unexpected token `newline'
sh: -c: line 0: `sed s/ol/ur/g < #<File:0xb7c6cb38> >
#<File:0xb7c6cb10>'

What happens?

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

def substitute(f,g,w,x)
    `sed s/#{w}/#{x}/g < #{f} > #{g}`
end

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

original file has
"
Hello folks or
old men
"
and canviada is empty

Thanks in advance,
Xan.

···

--
Posted via http://www.ruby-forum.com/\.

Paul Lutus wrote:

  File.open(dest) { |f| f.write(result) } if data != result

s/File.open(dest)/File.open(dest,"w")/

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

···

--
Paul Lutus
http://www.arachnoid.com

Paul Lutus wrote:

Paul Lutus wrote:

  File.open(dest) { |f| f.write(result) } if data != result

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

···

--
Posted via http://www.ruby-forum.com/\.

Xan Xann wrote:

Paul Lutus wrote:

/ ...

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. :slight_smile:

···

------------------------------------------

#!/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.

--
Paul Lutus
http://www.arachnoid.com

Paul Lutus wrote:

#!/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'

···

--
Posted via http://www.ruby-forum.com/\.

Paul Lutus wrote:

Xan Xann wrote:

Paul Lutus wrote:

/ ...

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. :slight_smile:

------------------------------------------

#!/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.

···

--
Posted via http://www.ruby-forum.com/\.

Eero Saynatkari wrote:

Paul Lutus wrote:

#!/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.

···

--
Posted via http://www.ruby-forum.com/\.

Xan Xann wrote:

/ ...

#!/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?

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.

···

--
Paul Lutus
http://www.arachnoid.com

Xan Xann wrote:

Eero Saynatkari wrote:

# 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.

···

--
Posted via http://www.ruby-forum.com/\.

Paul Lutus wrote:

Xan Xann wrote:

/ ...

temp.txt = "This is a test line."

  result = data.gsub(/#{find}/,replace)
  File.open(dest,"w") { |f| f.write(result) } if data != result
end

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

What happens?

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.

···

--
Posted via http://www.ruby-forum.com/\.

Eero Saynatkari wrote:

Xan Xann wrote:

Eero Saynatkari wrote:

# 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.

···

--
Posted via http://www.ruby-forum.com/\.