perl -Mstrict \ -ne'/regex(?{print @@; exit})/;' \ -e'shift(@@) if push(@@,$_)>5' file