Need a Python script

I work on various messages received from server and want to write a python script that can sort messages with unique flag values and give me the output in a text file.
I get these messages in the form of .zcap file from server, in order to get messages from those files; I use an internal tool:

  1.   To filter the zcap file and get security types,
    
  2.   To filter each security type to get various exchanges and
    
  3.   To filter each exchange for message types.
    

[CENTER]BOND
([/CENTER]

For eg: I filtered a .zcap file to get message types as -

[CENTER]CVE

NYSE

TSE[/CENTER]

Each exchange have different message types like:
CVE- MTR_BOND, MTFD_BOND, MTQ_BQUOTE
NYSE- MTR_BOND,MTQ_MBBOQUOTE, MTA_RECAP,
TSE- MTR_BOND, MTFD_BOND, MTT_TRADE, MTT_STATUS, MTA_CLOSE

I have a unix command to run at message type level that generates 1 file of each unique flag value messages.
$ awk '/MTR/,/Close Time/{a[i\+\+]=$0;if($0~/Flags:/){sub(":","",$2);fname=$2}if($0~/Close Time/){for(j=0;j<=i;j++)print a[j] >> fname;i=0}}' input.txt

But this is a tedious job to run this command for each message type (for eg; MTR_BOND) and create files for unique flag value messages. I want a script that I can run at Security type level (for eg; Bond)
Here I attach text file for BOND and its exchanges and message types. Please write a python script (preferably) to generate 1 text file for each message types with only 1 message of unique flag value.