use strict; use warnings; local $/ = "A red line is found\n"; while () { print "Red line $1" if /(at location[^\n]*\n)/; } __DATA__ A red line is found at location 2.5 4.5 A Blue line is found at location 2.6 4.5 A red line is found at location 2.67 4.56 The red arc is found at location 2.5 4.5 #### Red line at location 2.5 4.5 Red line at location 2.67 4.56