perl -i -0777 -pe 's/(\bAAAA )(QQQQ)(PPPP)( BBBB\b.*?\bCCCC XXXX )\2\3\b/$1$2$3$4$2/sg' /tmp/f