Python : Extract one string from 100 lines of text
Question:   1. I need to extract a particular string from 100 lines of log data. I tried split and then tried to get the needed string but couldn't succeed. Any suggestions/help appreciated. Thanks!

In the log below, I would like to extract the highlighted part, that is
**zqn.2005- 04.com.sanblaze:virtualun.init74-2.initiator-00000000-0000** (its
in line 57)

    
        1         Out[19]:
    2         {'portstatus': {'errors': {'busy_errors': '0',
    3         'checkcondition_errors': '0',
    4         'compare_errors': '0',
    5         'ioc_errors': '0',
    6         'notready_errors': '0',
    7         'read_errors': '0',
    8         'read_retries': '0',
    9         'scsi_errors': '0',
    10         'test_errors': '0',
    11         'timeout_errors': '0',
    12         'write_errors': '0',
    13         'write_retries': '0'},
    14         'net_counters': {'RxBytes': '148476788547060',
    15         'RxCompressed': '0',
    16         'RxDrop': '96188',
    17         'RxErrs': '0',
    18         'RxFIFO': '0',
    19         'RxFrame': '0',
    20         'RxMulticast': '259513',
    21         'RxPFCPause': '0',
    22         'RxPackets': '77165581759',
    23         'RxStandardPause': '0',
    24         'TxBytes': '20440169002909',
    25         'TxCompressed': '0',
    26         'TxDrop': '0',
    27         'TxErrs': '0',
    28         'TxFIFO': '0',
    29         'TxFrame': '0',
    30         'TxMulticast': '0',
    31         'TxPFCPause': '0',
    32         'TxPackets': '55075507366',
    33         'TxStandardPause': '5349727',
    34         'net_avgriops': '0',
    35         'net_avgrrate': '0.00',
    36         'net_avgtiops': '0',
    37         'net_avgtrate': '0.00',
    38         'net_riops': '0',
    39         'net_rrate': '0.00',
    40         'net_tiops': '0',
    41         'net_trate': '0.00'},
    42         'perfdata': {'avg_oiocnt': '0',
    43         'avgiops': '0',
    44         'avgriops': '0',
    45         'avgrrate': '0.00',
    46         'avgtrate': '0.00',
    47         'avgwiops': '0',
    48         'avgwrate': '0.00',
    49         'iops': '0',
    50         'max_oiocnt': '509',
    51         'oiocnt': '0',
    52         'riops': '0',
    53         'rrate': '0.00',
    54         'trate': '0.00',
    55         'wiops': '0',
    56         'wrate': '0.00'},
    57         'status': {'initiator0_iqn': 'zqn.2005- 04.com.sanblaze:virtualun.init74-2.initiator-00000000-0000',
    58         'initiator10_iqn': 'zqn.2005-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0010',
    59         'initiator11_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0011',
    60         'initiator12_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0012',
    61         'initiator13_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0013',
    62         'initiator14_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0014',
    63         'initiator15_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0015',
    64         'initiator16_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0016',
    65         'initiator17_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0017',
    66         'initiator18_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0018',
    67         'initiator19_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0019',
    68         'initiator1_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0001',
    69         'initiator20_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0020',
    70         'initiator21_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0021',
    71         'initiator22_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0022',
    72         'initiator23_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0023',
    73         'initiator24_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0024',
    74         'initiator25_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0025',
    75         'initiator26_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0026',
    76         'initiator27_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0027',
    77         'initiator28_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0028',
    78         'initiator29_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0029',
    79         'initiator2_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0002',
    80         'initiator30_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0030',
    81         'initiator31_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0031',
    82         'initiator32_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0032',
    83         'initiator33_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0033',
    84         'initiator34_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0034',
    85         'initiator35_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0035',
    86         'initiator36_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0036',
    87         'initiator37_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0037',
    88         'initiator38_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0038',
    89         'initiator39_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0039',
    90         'initiator3_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0003',
    91         'initiator40_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0040',
    92         'initiator41_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0041',
    93         'initiator42_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0042',
    94         'initiator43_iqn': 'iqn.2003-04.com.sanblaze:virtualun.init74-2.initiator-00000000-0043',
    95         'mode': 'Init',
    96         'numinitiators': '50',
    97         'numtargets': '0',
    98         'port': '0',
    99         'portwwnn': '90:e2:ba:82:92:1c',
    100         'portwwpn': '90:e2:ba:82:92:1c',
    101         'speed': '10G',
    102         'state': 'Online',
    103         'topo': 'iSCSI'},
    104         'sympn': '-',
    


Answer: Looks like the string is in dictionary format (correct me if I'm wrong), so
you could try converting it to one. Then you won't need regex.

    
    
    import ast
    your_dict = ast.literal_eval(your_string)
    

Then what you want would be:

    
    
    your_dict['status']['initiator0_iqn']
    

