perl -wMstrict -i -nle 's/PAT/REPL/g and $a++;print}{print $a//0' FILE