Oct-10-2021, 11:25 AM
Do a test to make sure username is in line,as your regex dos not test that.
Use
Then it will be like this.
Use
enumerate
and not range(len(data))
.Then it will be like this.
import re import json from ast import literal_eval filename = "systemfile.log" # strip unneeded text from json format and save audit lines only re_line= re.compile("audit") data = [] with open(filename, "r") as in_file: # Loop over each log line for line in in_file: if re_line.search(line): data.append(line) #print(data) for index,line in enumerate(data): if 'username' in line: res = data[index].strip().split('[audit] ')[1] result = literal_eval(res) #print(result) timestamp = result['timestamp'] user = result['user']['username'] print(timestamp) print(user)
Output:1633848664540
admin
1633848914062
admin