perl -ne "m/foo/ and die @@ ; shift(@@) if push(@@,$_)>5" myfile.txt #### perl -Mstrict -ne "m/addr/ and eval {print @@; exit}; shift(@@) if push(@@,$_)>5" myfile.txt