我有一个文件,其中包含以下格式的行。
{'This.is', 'string_1/test/testing_for/work', '/learn/word_in'}
{'This.is', 'string/test/splitting_for/work', '/learn/word_in'}
{'This.is', 'string_1/test/testing_for/work', '/learn/word_in'}
{'This.is', 'string/test/splitting_for/work', '/learn/word_in'}
我需要拆分这些行并仅打印splitting
,testing
的测试类型,如输入文件中所示(例如,从第一行开始,我只需要testing
作为输出)我也想打印测试类型,其中字符串长度等于7或8(不要打印splitting
测试类型)。
在上面我需要得到
的输出testing
testing
已尝试过类似下面的内容,但未提供我所期望的结果。
f = File.open("/root/tempfile.txt", "r")
f.each do |ln|
ln.chomp
fr = []
fn = []
fr = ln.split("/")
puts fr[2]
end
f.close
答案 0 :(得分:1)
输入文件:
{'This.is', 'string/test/splitting_for/work', '/learn/word_in'}
{'This.is', 'string/test/working_for/work', '/learn/word_in'}
{'This.is', 'string/test/nothing_for/work', '/learn/word_in'}
{'This.is', 'string/test/doing_for/work', '/learn/word_in'}
{'This.is', 'string_1/test/testing_for/work', '/learn/word_in'}
代码:
File.open("test.txt", "r").each do |l|
l.chomp
fr = l.split("/")
str = fr[2].split("_").first
str_len = str.length
puts str if str_len == 7 || str_len == 8
end
Out Put:
working
nothing
testing