my $printing = 0; while () { if (/Query=\s*(.*)/) { $printing = (index($headerline,$1) >= 0); } print if $printing; }