buf = [] hit = false for line in ARGF line.chomp! case line when /^BUFR_EDITION=/ then if hit buf.each{|ln| puts ln} buf = [] hit = false STDERR.puts "hit" else buf = [] STDERR.puts "miss" end buf.push line #when /^309051/ then when /IUKS01\\040NZKL/ then #when /^001002 997/ then hit = true buf.push line else buf.push line end end if hit buf.each{|ln| puts ln} end