def matchid (line, lineline)
m = /x:Key="(\S+)"/.match(line)
n = /x:Key="(\S+)"/.match(lineline)
if m == n
return true
else
return false
end
end
def matchcomment(line, lineline)
m = /<!-- (.+) -->/.match(line)
n = /<!-- (.+) -->/.match(lineline)
if m == n
return true
else
return false
end
end
def matchspace(line, lineline)
m = /(\s+)/.match(line)
n = /(\s+)/.match(lineline)
if m == n
return true
else
return false
end
end
matchline = 1
f = File.open('chi.xaml' , 'r')
file = File.open('eng.xaml' , 'r')
newFile = File.open('chinew.xaml' , 'w')
f.each_line do |line|
if matchline == 1
lineline = file.gets
else
lineline = lineline
end
if matchid(line, lineline) == true
newFile.puts(line)
matchline = 1
elsif matchcomment(line, lineline) == true
newFile.puts(line)
matchline = 1
elsif matchspace(line, lineline) == true
newFile.puts(line)
matchline = 1
else
matchline = 0
end
end
基本上,我想要比较两个具有相同ID的文件,但其中一个文件的订单搞砸了。 我想安排文件以匹配另一个的顺序,我尝试使用上面的代码,但似乎无法获得正确的输出。
文件1中的行:
<!-- VitalSignsView String -->
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CPU">CPU</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_RAM">RAM</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_MB">MB</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_OPEN_BRACKET">(</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CLOSE_BRACKET">b)</sys:String>
<!-- SetupButtonBarView -->
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_CONFIG_OVERVIEW">Config Overview</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTRUMENT">Instrument Setup</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_USER_PREFERENCE">User Preference</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_HW_SW_INFO">HW/SW Info</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTALLED_OPTION">Installed Options</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_EXIT">Exit</sys:String>
文件2中的行:
<!-- SetupButtonBarView -->
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_CONFIG_OVERVIEW">Config Overview</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTRUMENT">仪器设置</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_USER_PREFERENCE">用户喜好设置</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_HW_SW_INFO">硬件/软件信息</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTALLED_OPTION">已装选件</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_EXIT">退出</sys:String>
<!-- VitalSignsView -->
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CPU">CPU</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_RAM">RAM</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_MB">MB</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_OPEN_BRACKET">(</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CLOSE_BRACKET">b)</sys:String>
想要输出:
<!-- VitalSignsView -->
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CPU">CPU</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_RAM">RAM</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_MB">MB</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_OPEN_BRACKET">(</sys:String>
<sys:String x:Key="IDS_STRING_FSM_VITAL_SIGN_CLOSE_BRACKET">b)</sys:String>
<!-- SetupButtonBarView -->
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_CONFIG_OVERVIEW">Config Overview</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTRUMENT">仪器设置</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_USER_PREFERENCE">用户喜好设置</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_HW_SW_INFO">硬件/软件信息</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_INSTALLED_OPTION">已装选件</sys:String>
<sys:String x:Key="IDS_BUTTONBAR_FSM_SETUP_EXIT">退出</sys:String>
答案 0 :(得分:1)
unordered_file = File.read('unordered_file')
unordered_lines = unordered_file.each_line.reduce(Set.new) {|set, line| set << line}
ordered_file = File.read('ordered_file')
File.open('out', 'w') do |f|
ordered_file.each_line {|line| f.puts(line) if unordered_lines.contains?(line)}
end
假设ordered_file
答案 1 :(得分:1)
Josh,很好的解决方案,除了看起来他需要将线条与线条而不是整条线相匹配,因为内部文本有时会有所不同。用这样的方法替换File.open循环中的文本可能有效:
ordered_file.each_line do |line|
m = /x:Key="(\S+)"/.match(line)
f.puts(line) if m and unordered_lines.contains?(m[1])
end