hdr = nil phase = nil for line in ARGF line.chomp! case line when /^HEADER_STRING=(.*)/ then hdr=$1 when /^DATASUBSET 1 / then phase = 1 else if phase case line when /^(30905[012]|301001|001001)/ then when /^(001018)/ then when /^(301032)/ then else puts [hdr, line].join("\t") end phase = nil else end end end