I am looking for suggestions to create a regular expression based search in Python. I have got following type of string values in a server log file,
2017-03-18 13:24:05,791 INFO [STDOUT] SUB Request Status :Resubmitted INBIOS_ABZ824
2017-03-12 13:24:05,796 INFO [STDOUT] SUB Submit Status :Resubmitted INDROS_MSR656
2017-04-12 13:24:05,991 INFO [STDOUT] SUB Request Status :Resubmitted INHP_GSN848
and I need to search the log and extract the values like following,
2017-03-18 13:24:05,791 INBIOS_ABZ824
2017-03-12 13:24:05,796 INDROS_MSR656
2017-04-12 13:24:05,991 INHP_GSN848
I am using the following code, but its extracting the complete line where the strings like these are present (INBIOS_ABZ824). How can I extract only the specified values from the log as above, Please share your thoughts
What I have tried:
import os
import re
line_regex = re.compile(r"[A-Z]+TECH_[A-Z]+[0-9]+", re.IGNORECASE)
output_filename = os.path.normpath("output.log")
with open(output_filename, "w") as out_file:
out_file.write("")
with open(output_filename, "a") as out_file:
with open("ServerError.txt", "r") as in_file:
for line in in_file:
if (line_regex.search(line)):
print(line)
out_file.write(line)