my @lines; while(@lines < $n) { @lines = grep/criteria/,split/\n/,`tail -n $i file.foo`; $i+=$n; }