Help!! manipulating file

Hi all,

I need help manipulating the file below.
Here is what I needed to do.
First, I have to replace INSUPD to DELETE.
Then I need to change the content of the file around by flipping the contents in the file from the bottom to the top (start from "CMD")
How should I attack this?

Here is the original file.

SET DECIMAL ,
SET DATAFORMAT DELIMITED
SET SEPARATOR ~
SET PREFIX DATA NONE
SET QUOTE NONE
SET DATEFORMAT DD-MM-YYYY

CMD INSUPD format
ATT code nature_e
INC /usr/tmp/FORMAT.fmt

CMD INSUPD format_element
ATT sqlname_c
INC /usr/tmp/FORMAT.elt

CMD INSUPD denomination format_element
ATT object_id
INC /usr/tmp/FORMAT.feden

CMD INSUPD script_definition
ATT attribute.sqlname_c
INC /usr/tmp/FORMAT.def

Here is what I want it to be.

SET DECIMAL ,
SET DATAFORMAT DELIMITED
SET SEPARATOR ~
SET PREFIX DATA NONE
SET QUOTE NONE
SET DATEFORMAT DD-MM-YYYY

CMD INSUPD script_definition
ATT attribute.sqlname_c
INC /usr/tmp/FORMAT.def

CMD INSUPD denomination format_element
ATT object_id
INC /usr/tmp/FORMAT.feden

CMD INSUPD format_element
ATT sqlname_c
INC /usr/tmp/FORMAT.elt

CMD INSUPD format
ATT code nature_e
INC /usr/tmp/FORMAT.fmt

Here is what I got so far.
#/bin/ksh

# Replacing INSUPD with DELETE

/bin/sed -e "s:INSUPD:DELETE:" /usr/tmp/XYZ_FILE.cmd | awk '{print};/INC .*.del/{exit}' > /usr/tmp/ XYZ_FILE.del.cmd

#end script

By 'flipping around' if you mean fully reversing the order of and you are on linux try the tac command --

grep '^SET ' filename > newfile
sed -n '/^SET/,/$/p' filename | tac -  >> newfile

I want to move section 1 to 3 and via versa.
Here is an example:

1) CMD INSUPD format
ATT code nature_e
INC /usr/tmp/FORMAT.fmt

2) CMD INSUPD format_element
ATT sqlname_c
INC /usr/tmp/FORMAT.elt

3) CMD INSUPD denomination format_element
ATT object_id
INC /usr/tmp/FORMAT.feden