[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to replace file's content directly?

camenix

10/16/2006 10:49:00 AM

I have a sql file exported from mysql,and want to chang the charecter
set to utf-8.

sqltempfile=File.new(sqltemp,'w+') ### How to get rid of it?
File.open(sqlfile,'r+') do |file|
lines=file.gets(nil)
lines.gsub!(/TYPE=MyISAM/){|match| match='DEFAULT CHARACTER SET
utf8'}
sqltempfile.puts lines ### How to get rid of it?
end
sqltempfile.close ### How to get rid of it?

And the question is,how to replace the string directly without
sqltempfile?
Anyone help me?

4 Answers

Robert Klemme

10/16/2006 10:57:00 AM

0

On 16.10.2006 12:49, camenix wrote:
> I have a sql file exported from mysql,and want to chang the charecter
> set to utf-8.
>
> sqltempfile=File.new(sqltemp,'w+') ### How to get rid of it?
> File.open(sqlfile,'r+') do |file|
> lines=file.gets(nil)
> lines.gsub!(/TYPE=MyISAM/){|match| match='DEFAULT CHARACTER SET
> utf8'}
> sqltempfile.puts lines ### How to get rid of it?
> end
> sqltempfile.close ### How to get rid of it?
>
> And the question is,how to replace the string directly without
> sqltempfile?
> Anyone help me?
>

ruby -i -p -e 'gsub! /TYPE=MyISAM/, "DEFAULT CHARACTER SET utf8"' your_file

If you want backups, you can use "-i.bak" instead of "-i".

robert

camenix

10/16/2006 3:06:00 PM

0

perlish script.Ugly,but worked.
In windows/dos , I have to use "-i.bak" instead of "-i".
thanks.

matt

10/16/2006 6:08:00 PM

0

camenix <vipeak@gmail.com> wrote:

> > On 16.10.2006 12:49, camenix wrote:
> > > I have a sql file exported from mysql,and want to chang the charecter
> > > set to utf-8.
> > >
> > > sqltempfile=File.new(sqltemp,'w+') ### How to get rid of it?
> > > File.open(sqlfile,'r+') do |file|
> > > lines=file.gets(nil)
> > > lines.gsub!(/TYPE=MyISAM/){|match| match='DEFAULT CHARACTER SET
> > > utf8'}
> > > sqltempfile.puts lines ### How to get rid of it?
> > > end
> > > sqltempfile.close ### How to get rid of it?
> > >
> > > And the question is,how to replace the string directly without
> > > sqltempfile?
> > > Anyone help me?
> > >
> >
> > ruby -i -p -e 'gsub! /TYPE=MyISAM/, "DEFAULT CHARACTER SET utf8"' your_file
> perlish script.Ugly,but worked.

Would this be less "ugly"?

s = nil
open(myfile, 'r') {|f| s = f.read}
open(myfile, 'w') {|f| f.puts s.gsub("TYPE=MyISAM", "DEFAULT CHARACTER
SET utf8")}

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

camenix

10/17/2006 3:16:00 AM

0

> > >
> > > ruby -i -p -e 'gsub! /TYPE=MyISAM/, "DEFAULT CHARACTER SET utf8"' your_file
> > perlish script.Ugly,but worked.
>
> Would this be less "ugly"?
>
> s = nil
> open(myfile, 'r') {|f| s = f.read}
> open(myfile, 'w') {|f| f.puts s.gsub("TYPE=MyISAM", "DEFAULT CHARACTER
> SET utf8")}
>
I appreciate your help.