以下是输入文件中的示例数据。
File Id: C:/my_files/456_Milo_123-values.txt
#RakeBoss-Jan 21st Prod
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\kzhr2x, chk\, chk\czzjrt
jee_only = chk\zz9ycd, chk\hz659l
year_on = chk\dytg6
[/]
@met = rw
@div = rw
@jee_only = r
[/Release]
year_on = r
[/Redmine/Treehub]
@div = r
@jee_only = rw
我希望它像下面这样组织成输出文件
Name group id folder access
456_Milo_123 met chk\rel_io_chk / rw
456_Milo_123 met chk\dev_op_io / rw
456_Milo_123 div chk\kzhr2x / rw
456_Milo_123 div chk\czzjrt / rw
456_Milo_123 div chk\zz52t0 / rw
456_Milo_123 jee_only chk\zz9ycd / rw
456_Milo_123 jee_only chk\zz9ycd / rw
456_Milo_123 year_on chk\dytg6 /Release r
456_Milo_123 div chk\czzjrt /Redmine/Treehub r
456_Milo_123 div chk\zz52t0 /Redmine/Treehub r
456_Milo_123 jee_only chk\zz9ycd /Redmine/Treehub rw
456_Milo_123 jee_only chk\zz9ycd /Redmine/Treehub rw
我尝试了很多分割和拼接组合来组织它但不能实现上述输出。
我的不完整尝试
file = File.open("input.txt", "rb")
values = file.read.split("File Id:")
values.each do |val|
val.reject {||a| a.match(/^#|^\[/m)}
end
#not sure how to process further
match_by_id("input.txt") do |id, lines|
lines.each do |line|
group, group_values = line.split("=")
group.reject {|a| a.match(/^#|^\[/m)}
end
#not sure how to process further
end
values.shift
知道怎么做吗?感谢。
输入文件
File Id: C:/my_files/00_Roll_Tom-values.txt
#RakeBoss-Random as on 12/19/2016
[groups]
met = chk\rel_io_chk, chk\dev_op_io,
div = chk\kzhr2x, chk\zz52t0, chk\czzjrt
rakeonly = chk\rzgnsd, chk\cztw5h
[/]
@met = rw
@div = rw
@rakeonly = r
*******************************************************************************************
File Id: C:/my_files/Rander-values.txt
#RakeBoss-Jan 21st QA
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\541kmj, chk\zz52t0
app_only = chk\zz9ycd
check_io = chk\wder4, chk\zz9ycd
div_write = chk\lo98j3
year_on = chk\3w345f
[/]
@met = rw
@div = rw
@app_only= r
@check_io = r
@div_write = rw
@year_on = r
[/wedmin]
@check_io = rw
@div_write= rw
[/doc/prod]
@div = rw
@app_only = r
@year_on = rw
*******************************************************************************************
File Id: C:/my_files/456_Milo_123-values.txt
#RakeBoss-Jan 21st Prod
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\kzhr2x, chk\, chk\czzjrt
jee_only = chk\zz9ycd, chk\hz659l
year_on = chk\dytg6
[/]
@met = rw
@div = rw
@jee_only = r
[/Release]
@year_on = r
[/Redmine/Treehub]
@div = r
@jee_only = rw
******************************************************************************************
File Id: C:/my_files/Docker_red-values.txt
#RakeBoss-Check it io.
[groups]
met = chk\rel_io_chk, chk\dev_op_io, chk\njk987
div = chk\gzlqvg, chk\zzkgyp, chk\lzg0rp, chk\gzp2kv
rakeonly = chk\qzvjq0, chk\kz6w6t, chk\bzy4kj, chk\dzfbhp
[/]
@met = rw
@div = rw
@rakeonly = r
******************************************************************************************
答案 0 :(得分:0)
我开始按块拆分。
file = File.open("input.txt", "rb")
blocks = file.read.split(/\n\s*\n/)
每个街区都需要不同的处理......
name = parse_name blocks[0]
groups = parse_groups blocks[1]
....
parse_groups的想法是在[groups]
和[/]
内部进行阻止并将其解析为split(/^([^\s]+)\s*=\s*([^,\s])/)
...