Reading files and regex with PYTHON
Question: i'm beginner in python and in programming in general, I'd like to create a
script in python that can tell if the message was sent or not. after reading
log files I've noticed that each message has a **mid** so i got an idea but
i'm not sure... if i save all the mid in a list or dictionary i'll be able to
compare it with the line that has the mid+**\s** +"**OK** " or "**Error** "
here's a part of my log file ( the mid's are incremented by one each time. )
can someone help me out to start ? because i have no idea how to start this..
Thanks in advance

    
    
    01:09:25.258 mta    Messages       I Doc O:NVS:SMTP/a.b@x.de R:NVS:SMS.0/+01542xx **mid:6261**
    01:09:41.965 mta    Messages       I Rep O:NVS:SMTP/a.b@x.de R:NVS:SMS.0/+01542xx **mid:6261**
    01:09:41.965 mta    Messages       I Rep **6261** OK, Accepted (ID: 26)
    


Answer: I would suggest the following:

Let's assume that the log file isn't too big to fit in memory.

    
    
    import re
    with open("myfile.log") as infile:
        data = infile.read()
    

First, find all the message IDs in your file:

    
    
    messages = set(m.group(1) for m in re.finditer(r"\*\*mid:(\d+)\*\*", data))
    

Now, find all the message IDs for which there exists an `OK` line

    
    
    successes = set(m.group(1) for m in re.finditer(r"\*\*(\d+)\*\*\s*OK", data)) 
    

The difference between the two is the set of IDs that have no `OK` message:

    
    
    failures = messages - successes
    

