my %seen; while () { %seen = () if (/^\s*STORE/); #start over for each STORE #just /\f/ might be fine too next if $seen{$_}; $seen{$_}++ if /^DEPT/; #no more DEPT lines print; }