We have a large log file containing following two lines:
00 LOG | Cycles Run: 120001
00 LOG ! Virtual: Max> ?????????? bytes (?.???? gb), Current> 640733184 bytes (?.???? gb).
00 LOG ! Virtual: Max> 1082470400 bytes (?.???? gb), Current> ????????? bytes (?.???? gb).
00 LOG ! Actual: Max> ????????? bytes (?.???? gb), Current> 472154112 bytes (?.???? gb).
00 LOG ! Actual: Max> 861736960 bytes (?.???? gb), Current> ????????? bytes (?.???? gb).
As the log file is big in size, we want to read line by line(not to read whole text in a buffer at a time), match specific set of patterns and pick values in separate variables.
e.g.
00 LOG | Cycles Run: 120001
We want o pick 120001
and store in a variable say cycle
.
On the other hand we parse these lines:
00 LOG ! Virtual: Max> ?????????? bytes (?.???? gb), Current> 640733184 bytes (?.???? gb).
00 LOG ! Virtual: Max> 1082470400 bytes (?.???? gb), Current> ????????? bytes (?.???? gb).
00 LOG ! Actual: Max> ????????? bytes (?.???? gb), Current> 472154112 bytes (?.???? gb).
00 LOG ! Actual: Max> 861736960 bytes (?.???? gb), Current> ????????? bytes (?.???? gb).
Characters marked with ?
can be any digit.
We want to store vairables like followings:
640733184
in varvirtual_cur
1082470400
in varvirtual_max
472154112
in varactual_cur
861736960
in varactual_max
Written a snippet in Python 3.6
but it's printing empty list:
import re
filename = "test.txt"
with open(filename) as fp:
line = fp.readline()
while line:
cycle_num = re.findall(r'00 LOG | Cycles Run: (.*?)',line,re.DOTALL)
line = fp.readline()
print (cycle_num[0])
NOTE: I want to pick each values in seperate variables and use it later on. Need to set 5 patterns one by one, pick value if it matches any specific pattern and put it inrespective variable.
Not sure about the wildcard matching for the second pattern.
Please suggest us a way to do this efficiently.