I have a python script that I'm using to parse a log file and print out matching errors, etc. What I'd like to do is also print the line number that the matching entry was found on.
I've seen a few similar posts such as; Search File And Find Exact Match And Print Line? But I haven't been able to apply the examples successfully.
My code is:
from sys import argv
script, filename = argv
with open(filename) as f:
data = f.read().splitlines()
# Process counts and matches of events:
assertmatch = [s for s in data if "Assertion" in s]
assertcount = len(assertmatch)
if assertcount > 0:
print " "
print "ASSERTIONS:"
print '\n'.join([str(myelement) for myelement in assertmatch])