请问Ruby如何实现复制文件同时数据转换

2024-12-16 13:01:54
推荐回答(1个)
回答1:

fin = File.new("C:\\1.txt")
tmp = []

fin.each_line do |line|
if line =~ /include.*?xercesc.*?>/
line.sub!(/(.*)<.*\/(.*?)>(.*)/, '\1"\2"\3')
end
tmp << line
end

fin.close
# puts tmp.join()
fout = File.new("C:\\2.txt", "w")
fout.write(tmp.join())
fout.close

测试输入文件1.txt:
this is the first line

xy zd

测试输出文件2.txt:
this is the first line
"include abc_xercesc.h"
xy "include 123_xercesc.c" zd