use strict; use warnings; while () { next unless /^internal name/ .. 0; next unless /^need this/; # your code here... print $_; } __DATA__ a line need this (not really) another line internal name another line a further line need this another line need this