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

Python014

请问Ruby如何实现复制文件同时数据转换,第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

</include abc_xercesc.h>

xy <aaa/include 123_xercesc.c>zd

测试输出文件2.txt:

this is the first line

"include abc_xercesc.h"

xy "include 123_xercesc.c" zd

脚本语言, 顾名思义就是一般程序写出来比较小. 是个小脚本, 可以完成特定的功能. 脚本语言可以处理文档, 可以生成报表, 可以生成其他脚本, 可以建立其他语言的工作环境, 可以做许多事情. 甚至可以完成底层语言能完成的所有事情. tcl也是一种语言. 与工具结合的更紧密, 很多工具可以直接识别tcl,而perl和ruby就可以用来批量生成tcl脚本. 所以它们可以相辅相成的协调工作.