0

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!

asked Nov 24, 2014 at 10:44

1 Answer 1

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.

http://regex101.com/r/yP3iB0/21

answered Nov 24, 2014 at 10:48
4
  • 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 . . . Commented 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. Commented Nov 24, 2014 at 11:59
  • @user3636424 this code will work in python3 as well.Just remove the u thing.Use the updated code Commented 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? Commented Nov 24, 2014 at 14:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.