1

I have the following example text file (it is in the format as indicated below). I want to extract everything between the lines "Generating configuration...." and "`show accounting log all`", this is the beginning and end of what I am interested in.

some lines
some more line
Generating configuration....
interested config
interested config
interested config
`show accounting log all`
some lines
some more line

I wrote the following code, but its does not stop appending the lines to the textfile after it has found `show accounting log all`.

    config_found = False
    with open(filename, 'rb') as f:
        textfile_temp = f.readlines()

    for line in textfile_temp:
        if re.match("Generating configuration....", line):
            config_found = True
        if re.match("`show accounting log all`", line):
            config_found = False
        if config_found:
            i = line.rstrip()
            textfile.append(i)

what am i doing wrong with my statements?

1
  • 4
    Looks like in your example content they are backticks around show accounting log all and in your code it's looking for single quotes, so it will never match. (Why are you using the regex module re, instead of plain string comparison?) Commented Mar 26, 2014 at 3:52

2 Answers 2

3

Instead of single quotes, you have to use back quote in your comparision and you can have if and elif for extracting in between strings. I have modified as below and it's working:

with open('file.txt', 'rb') as f:
    textfile_temp = f.readlines()
    config_found = False
    textfile = []
    for line in textfile_temp:
        if re.match("`show accounting log all`", line):
            config_found = False
        elif config_found:
            i = line.rstrip()
            textfile.append(i)
        elif re.match("Generating configuration....", line):
            config_found = True
    print textfile

Output:

  ['interested config', 'interested config', 'interested config']

Instead you can use split as below:

 with open('file.txt', 'rb') as f:
     textfile_temp = f.read()
     print textfile_temp.split('Generating configuration....')[1].split("`show accounting log all`")[0]

Output:

interested config 
interested config 
interested config 
Sign up to request clarification or add additional context in comments.

3 Comments

Just to add on, the "backtick" character can be found on the keyboard usually just above the "tab" key, on the same key as "tilde" (~) key.
user3 thanks this worked! What was wrong with the if statements I had in my original code (I had fixed the backticks and it still failed)? I can't seem to understand why it wouldnt have worked.
you are loading the result variable after that you are changing config_found variable. that should be reversed.
0

config_found appears to have no scope outside of the loop.

Put config_found = False before the loop and it should work fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.