Reputation: 1
I have some programming experience, but I'm new to python. I have an output file that I am looking to parse. There are two states that a computer could be in:
I'm looking to write a simple program that takes the output file and creates two lists...one with computers with status #1 and the other with computers with status #2.
The output file looks like this:
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: AAAAA
ERROR: A compatible Trusted Platform Module (TPM) was not detected.
---
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: BBBBBB
Upvotes: 0
Views: 114
Reputation: 56654
from collections import defaultdict
import re
def get_computer(row, reg=re.compile('Computer Name: (\S+)')):
m = reg.match(row)
return m and m.group(1)
def line_starts_with(s):
return lambda line: line.startswith(s)
found = line_starts_with('ERROR: The TPM is already on.')
not_found = line_starts_with('ERROR: A compatible Trusted Platform Module (TPM) was not detected.')
def process_states(inf, value_fn, state_fns):
states_matched = defaultdict(list)
value = None
for row in inf:
nv = value_fn(row)
if nv:
value = nv
else:
for state_label,state_fn in state_fns.items():
if state_fn(row):
states_matched[state_label].append(value)
break
return states_matched
def main():
with open('input.log') as inf:
results = process_states(inf, get_computer, {'found':found, 'not_found':not_found})
if __name__=="__main__":
main()
then for testing, I ginned up the following:
import StringIO
inf = StringIO.StringIO("""
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: AAAAA
ERROR: A compatible Trusted Platform Module (TPM) was not detected.
---
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: BBBBBB
ERROR: The TPM is already on.
---
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: CCCCCC
ERROR: The TPM is already on.
---
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.
Computer Name: DDDDDDD
ERROR: A compatible Trusted Platform Module (TPM) was not detected.
""")
results = process_states(inf, get_computer, {'found':found, 'not_found':not_found})
which returns
{'found': ['BBBBBB', 'CCCCCC'], 'not_found': ['AAAAA', 'DDDDDDD']}
Upvotes: 0
Reputation: 2804
How about something that tracks the current computer as a state, and then checks the ERROR message.
Untested code -
list1 = []
list2 = []
with open(file_name, 'r') as reader:
current_computer = None
for line in reader:
if line.startswith('Computer Name: '):
current_computer = line[len('Computer Name: '):]
if line.startswith('ERROR: A compatible Trusted Platform Module (TPM) was not detected.'):
list1.append(current_computer)
elif line.startswith('ERROR: The TPM is already on.'):
list2.append(current_computer)
Upvotes: 1