my @file = ; for(@file) { push @lines,$_ if /criteria/; last if @lines>$n; }