Skip to content
Advertisement

After a match of a string in a file, how to extract the next set of lines and continue iteration

Sample data:-

Group Information:
Name                      Target     Status   Role       Mode     Options
SG_hpux_vgcgloack.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV              ID   RemoteVV             ID   SyncStatus    LastSyncTime
  vgcglock_SG_cluster 13496 vgcglock_SG_cluster 28505 Synced        NA

Name                Target     Status   Role       Mode     Options
aix_rcg1_AA.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV         ID   RemoteVV      ID   SyncStatus    LastSyncTime
  tpvvA_aix_r.2  20149 tpvvA_aix.2  41097 Synced        NA
  tpvvA_aix_r.3  20150 tpvvA_aix.3  41098 Synced        NA
  tpvvA_aix_r.4  20151 tpvvA_aix.4  41099 Synced        NA


Name                Target     Status   Role       Mode     Options
aix_rcg2_AA.r518634 s2976      Started  Primary    Sync     auto_recover,auto_failover,path_management,auto_synchronize,active_active
  LocalVV         ID   RemoteVV      ID   SyncStatus    LastSyncTime
  decoA_aix_r.11 20158 decoA_aix.11 41106 Synced        NA
  decoA_aix_r.12 20159 decoA_aix.12 41107 Synced        NA
  decoA_aix_r.13 20160 decoA_aix.13 41108 Synced        NA

I want to search for line “Name” and the immediate next line and use it as Key: Value.

Code:-

##The file is large and the code not shown here extract the data from Group Information line
##and saves to "no_extra_lines.

# here i am removing the empty lines or empty strings
no_extra_lines = [line for line in required_lines if line.strip() != ""]
print(no_extra_lines)
print(len(no_extra_lines))


#here i want to iterrate over the string and want to extract the line "Name" and the immedite next line.
for num, line in enumerate(no_extra_lines):
    print(num, line)
    if "Name" in line:
        print(line)
        print(line +1)  
    

How to print the line and the next line? OR to put it in another way, how can I extract the next set of lines after every occurrence of “Name”. The list is huge with the same pattern. I want to extract these 2 lines for every occurance and save as a key-value.

Advertisement

Answer

Since you have said that ‘The list is huge’, I would suggest to use an approach that uses iteration only and avoids indexing:

def extract_name_next(lines):
    it = (line for line in lines if line.strip() != '')
    for line in it:
        if line.startswith('Name'):
            key = line
            try:
                value = next(it)
                yield key, value
            except StopIteration:
                raise ValueError('Name must be followd by additional line')

This is a generator function that yields (key, value) pair. To print you can use it like

for key, value in extract_name_next(required_lines):
    print(key)
    print(value)

You could also construct a dict from these pairs:

mydict = dict(extract_name_next(required_lines))

Note that you could aswell use this generator to extract key,value pairs from e.g. a file, even if the file does not fit into RAM:

with open('huge_file') as file:
    mydict = dict(extract_name_next(file))
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement