Tips to match multiple patterns from from a single file .

woooee at gmail.com woooee at gmail.com
Sun Jul 23 16:27:50 EDT 2017


You want to find strings on multiple lines of a file, and possible multiple string groups within a file (you didn't say). So you will have to check each line and store anything you want to keep for each file, and you will have to add the code to cycle through the files, so it is something along the lines of.

def find_a_string(string_in, rec):
    return_str=""
    string_len=len(string_in)
    if string_in in rec:
        location=rec.find(string_in)
        start=location+string_len
        ## go until you find a space or end of line
        for letter in rec[start:]:
            if len(letter.strip()):
                return_str += letter
            else:
                return return_str
    return return_str

test_file=test_data.split("\n")  ## turn into data like file_handle.readlines()
found_dict={}
for rec in test_file:
    if len(rec.strip()):
        for str in ("offset=", "Data before corruption : ", "size="):
            found=find_a_string(str, rec)
            if len(found):
                found_dict[str]=found

        ## assume this always comes after the above strings in the file
        if "Corrupting disk object 6 at 1,1,25296896:8192" in rec:
                print "object 1,1,25296896:8192",
                for key in found_dict:
                    print key.strip(), found_dict[key],
                print



More information about the Python-list mailing list