I have a string “[ERROR]: test_case_blebleble – FAILURE – 1 hr 47 min” get from console log jenkins
In my console log, I have a lot of message like this. so I want to check if have any string like this format FAILURE – 1 hr 47 min then go to get this message. So, in python how we can get it please help me = thanks
Advertisement
Answer
You could use in below way:
# Give the pattern that you are searching: pattern = re.compile("[ERROR]: test_case_blebleble - FAILURE") # Give name of your file for line in open("yourfile.txt"): for match in re.finditer(pattern, line): print(line)
OR
with open(r"yourfile.txt") as infile: for line in infile: line=line.strip() if "- FAILURE - 1 hr 47 min" in line: print(line) infile.close()
EDIT
import re with open(r"yourfile.txt") as infile: for line in infile: line=line.strip() z=re.match("^"[[a-zA-Z]+]:s[a-zA-Z]+_[a-zA-Z]+[0-9]+s+-sFAILUREs-s(0?[0-9]|1[0-9]|2[0-3])shrs(0?[0-9]|[1-5][0-9])smin"",line) if z: print(line) infile.close()