[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

word substitution in file

Xan Xann

9/6/2006 10:36:00 PM

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

13 Answers

Jason Nordwick

9/6/2006 10:46:00 PM

0

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

9/7/2006 2:29:00 PM

0

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

Paul Lutus

9/7/2006 6:40:00 PM

0

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

Paul Lutus

9/7/2006 7:04:00 PM

0

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

Xan Xann

9/9/2006 5:48:00 PM

0

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

Paul Lutus

9/11/2006 5:42:00 PM

0

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

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

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

e

9/11/2006 7:53:00 PM

0

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

Xan Xann

9/12/2006 11:14:00 AM

0

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

Xan Xann

9/12/2006 11:17:00 AM

0

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

e

9/13/2006 12:36:00 AM

0

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