It's worth leaving here that this is my second day in Python and I'm not very pro at this language. Any low level suggestion and easy to understand would be much appreciate.
I would like to use a variable inside a regex in python. I have read this question How to use a variable inside a regular expression? without any luck in the answer.
Code:
import time
import re
dia = time.strftime('%b %d')
final = open('/root/final.txt', 'ab')
file = open('/var/log/syslog', 'rb')
for line in file:
if re.findall('kernel|\bNetworkManager\b', line):
if re.findall(r'dia', line):
final.write(line)
There's a lot of code that I don't think is relevant to the question.
I have tried this solution as well if re.findall(r'%s'%dia, line)
with no lucky.
Since I'm in here i would like to explain my thought and see if I'm in the right direction:
- Open syslog
- Look for the word kernel and NetworkManager
- If the beginning of the line is today write to final.
Thanks in advance. Wish you a great year.