Python script to run multiple command and append data in output csv file

Experts,
I am writing a script and able to write only small piece of code and not able to collect logic to complete this task.

In input file have to look for name like like this (BGL_HSR_901_1AG_A_CR9KTR10) before sh iss neors. Record this (BGL_HSR_901_1AG_A_CR9KTR10) in csv file
Now have to grep field with names like(BGL_MWN_901_1AC_T_CR9R1) line above IS-extended, pick one of it suppose(BGL_RGN_901_1AC_T_CR9R5) also record this in output csv file.

Now from the next two Is-extended grep field (BGL_CCG_901_1AG_CR9R13.00) also record it in csv file and BGL_CCG_902_1AC_CR9R5 already taken so have to skip it.
Now from next two Is-extended grep field (BGL_CCG_902_1AC_CR9R14) also record it in csv file and BGL_CCG_902_1AC_CR9R13 already taken so have to skip it.
Now from next two Is-extended grep field (BGL_CCG_902_1AC_CR9KT04.00) also record it in csv file and BGL_CCG_902_1AC_CR9R14 already taken so have to skip it. Here we have to stop search when we have find field having "9K" in it and create row like below as in output file

Now again pick another value/field from line above IS-extended, record it in output file and repeat the same process and build another row in output file
Now, In line IS-extended field (BGL_CCG_901_1AG_CR9K41) have 9K in it. so have to pick it and record in csv file and stop search

Now again pick another value/field from line above IS-extended and repeat the same process and build another row in output file

output csv file:

BGL_HSR_901_1AG_A_CR9KTR10,BGL_RGN_901_1AC_T_CR9R5,BGL_CCG_901_1AG_CR9R13,BGL_CCG_902_1AC_CR9R14,BGL_CCG_902_1AC_CR9KT04
BGL_HSR_901_1AG_A_CR9KTR10,BGL_VKP_901_1AC_CR9R1,BGL_CCG_901_1AG_CR9K41
BGL_HSR_901_1AG_A_CR9KTR10,BGL_HSR_943_1AC_B_CR9R7

Input file

RP/:BGL_HSR_901_1AG_A_CR9KTR10#sh iss neors 
    
    BGL_MWN_901_1AC_T_CR9R1 0/0/        *P*         U    
    BGL_VKP_901_1AC_CR9R1 0/1/        *P*         U          
    BGL_RGN_901_1AC_T_CR9R5 0/1/        *PP*         p        
       
    BGL_HSR_943_1AC_B_CR9R7 /0/7        *P*         p     
    BGL_HSR_943_1AC_B_CR9R7 /0/4        *P*         p           
    BGL_HSR_944_1AC_B_CR9R5 
    
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9R5.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9R13.00
    
    
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9R14.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9R13.00  
    
     
      Metc: 10         IS-Extended BGL_CCG_902_1AC_CR9KT04.00
      Meic: 10         IS-Extended BGL_CCG_902_1AC_CR9R14.00 
    
      Metc: 10         IS-Extended BGL_VKP_901_1AC_CR9R1.00
      Meic: 10         IS-Extended BGL_CCG_901_1AG_CR9K41.00

My code :

 import csv
    import os
    import re
    
    
    data = []
    tmp = ''
    output = ''
    fieldnames = ("first")
    
    infile = open("sample.txt", "r")
    #outfile = open("outfile.csv", "w")
    
    for datas in infile:
        datas = datas.rstrip("\n")
        a = re.search(r"([A-Z]+_[A-Z]+_[0-9]+_[0-9][A-Z]+\D*_[A-Z]+[0-9]+[A-Z]+[0-9]+)", datas)
        if a:
            a = a.group(1)
            tmp = a
            output = output+'\n'+tmp
            data = output
        with open('your_file.csv', 'w') as f:
    #        for item in output:
    #        print >> f, item
                f.writelines(data)