nowgiven the preceding log filethe problem we need to solve is how to obtain the serial number of any drives that have xfs errors on them this serial number might later be used by data center technician to identify and replace the drive we know we can identify the individual lines using regular expressionsbut we'll have to change the regular expressions as we loop through the linessince we'll be looking for different things depending on what we found previously the other difficult bit is that if we find an error stringthe information about which bus contains that stringand what serial number is attached to the drive on that bus has already been processed this can easily be solved by iterating through the lines of the file in reverse order before you look at this examplebe warned--the amount of code required for coroutine-based solution is scarily smallimport re def match_regex(filenameregex)with open(filenameas filelines file readlines(for line in reversed(lines)match re match(regexlineif matchregex yield match groups()[ def get_serials(filename)error_re 'xfs error (\[sd[ - ]\])matcher match_regex(filenameerror_redevice next(matcherwhile truebus matcher send'(sd \ +{*format(re escape(device))serial matcher send('{\(serial=([^)]*)\)format(bus)yield serial device matcher send(error_refor serial_number in get_serials('example_log log')print(serial_numberthis code neatly divides the job into two separate tasks the first task is to loop over all the lines and spit out any lines that match given regular expression the second task is to interact with the first task and give it guidance as to what regular expression it is supposed to be searching for at any given time