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!

pafpaf
  • 275
  • 2
  • 5
  • 12

1 Answers1

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

vks
  • 67,027
  • 10
  • 91
  • 124
  • 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 Nov 24 '14 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 Nov 24 '14 at 11:59
  • @user3636424 this code will work in python3 as well.Just remove the `u` thing.Use the updated code – vks Nov 24 '14 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 Nov 24 '14 at 14:19