I have a large log file. Every minute I add recent activity of the OS. It looks like:
#@#@#@#@#@#@#@
Time:12:00 PM, CPU:12.0,RAM:12334321,Network:1231231233,....
#@#@#@#@#@#@#@
Time:12:01 PM, CPU:14.0,RAM:12354621,Network:1239864833,....
#@#@#@#@#@#@#@
Time:12:02 PM, CPU:9.0,RAM:12398781,Network:1231598697,....
#@#@#@#@#@#@#@
I used "#@#@#@#@#@#@#@" as the separator. every time I open the file and write (add) the current status and then close it.
To calculate some of the parameters I need to know what was the last situation of the system, so I need to open the file and read the file in reverse until I reach the separator (#@#@#@#@#@#@#@). How can I read the file in reverse till specific character which in my case is the seperator (#@#@#@#@#@#@#@) and put it (the last records of OS) into a list or tuple.
Thanks!
1 Answer 1
import re
p = re.compile(r'#@#@#@#@#@#@#@(?!.*?#@#@#@#@#@#@#@)(.*)$', re.DOTALL)
test_str = "#@#@#@#@#@#@#@ \nTime:12:00 PM, CPU:12.0,RAM:12334321,Network:1231231233,....\n#@#@#@#@#@#@#@\nTime:12:01 PM, CPU:14.0,RAM:12354621,Network:1239864833,....\n#@#@#@#@#@#@#@\nTime:12:02 PM, CPU:9.0,RAM:12398781,Network:1231598697,....\n#@#@#@#@#@#@#@\nTime:12:02 PM, CPU:9.0,RAM:12398781,Network:1231598697,....\nasasdas\ndsa\nd\n\nasd"
re.findall(p, test_str)
Here instead of test_str
you can use file.read()
.See demo.
-
The code run on my mac perfectly, but when i run it in windows i get this error: File "Gtest.py", line 2 p = re.compile(ur'#@#@#@#@#@#@#@(?!.*?#@#@#@#@#@#@#@)(.*)$', re.DOTALL) ^ SyntaxError: invalid syntax Press any key to continue . . .pafpaf– pafpaf11/24/2014 11:30:09Commented Nov 24, 2014 at 11:30
-
My bad ! Your code works with python v2, I need a code for python v3 ! sorry, I will mention it in my Question. again sorry.pafpaf– pafpaf11/24/2014 11:59:44Commented Nov 24, 2014 at 11:59
-
@user3636424 this code will work in python3 as well.Just remove the
u
thing.Use the updated codevks– vks11/24/2014 12:01:42Commented Nov 24, 2014 at 12:01 -
You'd have to read the whole file in memory though. Isn't it better to keep a buffer of the last lines between two separators and keep reading until EOF?jadkik94– jadkik9411/24/2014 14:19:41Commented Nov 24, 2014 at 14:19
Explore related questions
See similar questions with these tags.