Searching with the RE module

For our first search, we will simply use the regular expression module to look for the terms we are looking for. We will use a simple loop to the following:

#!/usr/bin/env python3import re, datetimestartTime = datetime.datetime.now()with open('sample_log_anonymized.log', 'r') as f:   for line in f.readlines():       if re.search('ACLLOG-5-ACLLOG_FLOW_INTERVAL', line):           print(line)endTime = datetime.datetime.now()elapsedTime = endTime - startTimeprint("Time Elapsed: " + str(elapsedTime))

The result took about 6/100 of a second to search through the log file:

$ python3 python_re_search_1.py2014 Jun 29 19:21:18 Nexus-7000 %ACLLOG-5-ACLLOG_FLOW_INTERVAL: Src IP: 10.1 0.10.1,2014 Jun 29 19:26:18 Nexus-7000 %ACLLOG-5-ACLLOG_FLOW_INTERVAL: ...

Get Mastering Python Networking - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.