I have something likedsadjakldjskasafdsfdsfsffdsfsf**STGN**fdsfsdfdsfkjldsffdsfffdsfs**IRV**fdsfsand I want to get dsadjakldjskasafdsfdsfsffdsfkjldsffdsfffdsfsf**STGN**fdsfsdfdsfs**IRV**fdsfsbut only for an interval of lines (say line 40000 till end of file) (moving the line containing STGN right above the next IRV underneath it)Any idea how I can do this, or whether there's a better alternative for this than sed? 
Move matched line above a different matched line using sed
sed;awk
awk 'NR < 40000 {print; next}     /\*\*STGN\*\*/ {line = $0; next}     /\*\*IRV\*\*/ && line {print line; line=}     {print}'With sed:sed -e '40000,$!b' -e '/\*\*STGN\*\*/{h;d;}' -e '/\*\*IRV\*\*/{x;/./p;s/.*//;x;}'(they assume the STGN occurs before the IRV).