while () { if (/(.*$find.*)/) { $header = $1; } elsif (/^\s{38}\S*/) { print NEW "$header\t", $_; } }