Based on @Raffaele's answer, I made some corrections for working with any log file (skipping lines that do not start from the requested date, for example, the Jenkins console log). In addition, a Max / Min Threshold has been added to filter strings based on duration restrictions.
#!/usr/bin/env python import re from datetime import datetime MIN_THRESHOLD = 80 MAX_THRESHOLD = 100 regCompile = r"\w+\s+(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d).*" filePath = "C:/Users/user/Desktop/temp/jenkins.log" lastTime = None lastLine = "" with open(filePath, 'r') as f: for line in f: regexp = re.search(regCompile, line) if regexp: currentTime = datetime.strptime(re.search(regCompile, line).group(1), "%Y-%m-%d %H:%M:%S") if lastTime != None: duration = (currentTime - lastTime).seconds if duration >= MIN_THRESHOLD and duration <= MAX_THRESHOLD: print ("#######################################################################################################################################") print (lastLine) print (line) lastTime = currentTime lastLine = line f.closed
source share