how to replace file's content directly?

C

camenix

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?
 
R

Robert Klemme

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
 
C

camenix

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

matt neuburg

camenix said:
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.
 
C

camenix

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.
 

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
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top