0

I am trying to parse a log file for a specific string however, the log file has similar strings and when I use my for loop to search the file, it is grabbing the most recent sequence of those characters. Any ideas?

    search1 = xyz
    search2 = xy

    while file as f:
         for line in file:
             if search2 in line:
                 print(line)

the log file looks similar to this

asd asda asdaga asdga xy xyz <--- The results I am receiving is the xyz line printing, even though my search variable was xy. I am guessing this is happening because the sequence 'xy' is in 'xyz' but can I search for the exact sequence of a string?

Thanks,

1
  • if you're writing the log file yourself, a simple fix would be to use markers that aren't substrings of each other. or you could add punctuation like Err_xy_ vs Err_xyz_ Commented Jan 16, 2018 at 20:38

2 Answers 2

1

A simple trick would be to split() the line, (optionally make it a set), and then do the membership test:

for line in file:
    # If you want to do many tests, then 
    # creating a `set` might be a good idea
    # line = set(line.split())
    # if search2 in line:
    # ...
    if search2 in line.split():
        print(line)
Sign up to request clarification or add additional context in comments.

1 Comment

@Aiven Yes, thats why it is mentioned as optional.
0

Assuming you have a file such as this:

blah xy blah
xxyz 123
adfayx
dat xyz data

Then the following Python code uses regex to find xy:

import re

with open('file', 'r') as f:
    file_contents = f.read()


re_target = re.compile('\sxy\s')
results = re_target.findall(file_contents)
print results

All this does is look for the string xy with whitespace on either side of it. Let me know if you have any questions!

Comments

Your Answer

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.