awk 'NR < 40000 {print; next}
/\*\*STGN\*\*/ {line = $0; next}
/\*\*IRV\*\*/ && line {print line; line=""}
{print}'
com sed
:
sed -e '40000,$!b' -e '/\*\*STGN\*\*/{h;d;}' -e '/\*\*IRV\*\*/{x;/./p;s/.*//;x;}'
(eles assumem que o STGN ocorre antes do IRV).