1

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:

  1. Open syslog
  2. Look for the word kernel and NetworkManager
  3. If the beginning of the line is today write to final.

Thanks in advance. Wish you a great year.

Community
  • 1
  • 1
Bruno Francisco
  • 3,841
  • 4
  • 31
  • 61

1 Answers1

1

You can't reference a variable in a string. A string is just text, it has no knowledge of the namespace, and the interpreter does not resolve that for it.

Since your variable dia is a string, you can just use that in your call to re.findall:

if re.findall(dia, line):
    pass

or something similar:

if re.findall(r"{0}".format(dia), line):
    pass

As for that correctness of what you're doing, if format of the time stamp on the log is the same what you're using, it should be correct.

EDIT: if you are reading strings from the log, you need not (or shouldn't) open them as binary, i.e. the b flag

Guilherme
  • 721
  • 6
  • 13
  • You don't really need to specify the index of your format arguments. – Malik Brahimi Jan 02 '16 at 01:22
  • What does the `{0}` means? Sorry if it's just a simple/noobie question. – Bruno Francisco Jan 02 '16 at 01:26
  • 1
    It tells it to place the value of the first variable among the parameters provided in the function `format`. You can check out the doc for string formatting [here](https://docs.python.org/3.5/library/string.html#formatstrings). Reading the docs is always a good starting point, and you can usually get tips on ways to use the functions provided – Guilherme Jan 02 '16 at 01:28
  • I tried the solution but it didn't work. I'm guessing there's something wrong with way of thinking. I will start a new question since it's a whole new question i have to ask. Thank you Guilherme. I will link the question here if you would like to take a look. – Bruno Francisco Jan 02 '16 at 01:34
  • Yea, sure. You should try and debug your code. Make sure each step executes as it should. Check the format of the file you have as input, test that each regex line is working by feeding it a test string. Isolate each step to find out where things break. These things usually require a methodological approach. Good luck. – Guilherme Jan 02 '16 at 01:43
  • I have followed your steps and i determined the problem is at `if re.findall(r'dia', line):`. I have tried: `if re.findall(r"{0}".format(dia), line):` and `if re.findall(dia, line):` but it's not working out. I have never got with a debug. It's on my to do list. Could you land me a hand? – Bruno Francisco Jan 02 '16 at 01:50
  • 1
    My best guess is its the regex. Check that the regex works for a single line of the file. Copy a log entry (or make one up), check that format is as you expect on a python shell. For e.g. do days in the log have a 0 appended to them, i.e. Jan 2 vs Jan 02? Are the lines parsed as you expect them to when you read in binary mode? These are some of the hypothesis you can try to eliminate, before moving on to other hypothesis. It's pretty much trial-and-error – Guilherme Jan 02 '16 at 01:57
  • [SOLVED] define two variables: `day`and `month`. Gather them together in a variable called `date`. `date = '%s %d' % (month, day)`. After that i used @Guilherme trick: `if re.findall(r'{0}'.format(date), line)`. Make sure you use the library `from datetime import datetime`. Here's a link to something more detailed: [Get today time python](http://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/) Be aware of the `double space`at `date = '%s %d' % (month, day)` – Bruno Francisco Jan 02 '16 at 02:22