0

I have been struggling with a text file to perform conditional appending/extending certain text using Python. My apologies in advance if this is too basic and already been discussed here in someway or the other :(

Concerning the code (attached), I need to add statement "mtu 1546" under the statement containing "description" only if it doesn't exist. Also, I would like to be able add "description TEST" statement under interface statement (and/or above mtu statement, if available) only if doesn't pre-exist. I am using python 2.7.

Here's is my code:

import re

f = open('/TESTFOLDER/TEST.txt','r')
interfaces=re.findall(r'(^interface Vlan[\d+].*\n.+?\n!)',f.read(),re.DOTALL|re.MULTILINE)

for i in interfaces:
    interfaceslist = i.split("!")
    for i in interfaceslist:
        if "mtu" not in i:
            print i


f.close()

print statement works fine with the condition as it's able to print the interesting lines correctly, however my requirement is to add (append/extend) the required statements to the list so I can further use it for parsing and stuff. Upon, trying append/extend function, the interpreter complains it to be a string object instead. Here's the sample source file(text). The text files I will parsing from are huge in size so only adding the interesting text.

!
interface Vlan2268
 description SNMA_Lovetch_mgmt
 mtu 1546
 no ip address
 xconnect 22.93.94.56 2268 encapsulation mpls
!
interface Vlan2269
 description SNMA_Targoviste_mgmt
 mtu 1546
 no ip address
 xconnect 22.93.94.90 2269 encapsulation mpls
!
interface Vlan2272
 mtu 1546
 no ip address
 xconnect 22.93.94.72 2272 encapsulation mpls
!
interface Vlan2282
 description SNMA_Ruse_mgmt
 no ip address
 xconnect 22.93.94.38 2282 encapsulation mpls
!
interface Vlan2284
 mtu 1546
 no ip address
 xconnect vfi SNMA_Razgrad_mgmt
!
interface Vlan2286
 description mgmt_SNMA_Rs
 no ip address
 xconnect 22.93.94.86 2286 encapsulation mpls
!
interface Vlan2292
 description SNMA_Vraca_mgmt
 mtu 1546
 no ip address
 xconnect 22.93.94.60 2292 encapsulation mpls
!
4
  • Have you noticed that you were using the variable i in both for loops? :) Commented Aug 5, 2011 at 18:39
  • Actually, Python knows it's a different variable, but to the human reader it's confusing. Commented Aug 5, 2011 at 18:46
  • Thanks Michael. It's what I had in mind too.. Commented Aug 5, 2011 at 18:59
  • Actually, it is the same variable. It only works because the inner loop is at the end of the body of the outer loop. If you used i in the outer loop after finishing the inner loop, its value would have been the last element of interfaceslist. Commented Aug 5, 2011 at 23:19

2 Answers 2

1

The basic answer to your question is very simple. Strings are immutable, so you can't append to or extend them. You have to create a new string using concatenation.

>>> print i
interface Vlan2286
 description mgmt_SNMA_Rs
 no ip address
 xconnect 22.93.94.86 2286 encapsulation mpls

>>> print i + ' mtu 1546\n'
interface Vlan2286
 description mgmt_SNMA_Rs
 no ip address
 xconnect 22.93.94.86 2286 encapsulation mpls
 mtu 1546

Then you have to save the result, either to a variable name or some kind of container. You could just save it to i like so:

i = i + ' mtu 1546\n'

or like so:

i += ' mtu 1546\n'

But in this case, a list comprehension might be useful...

def add_mtu(i):
    return i if "mtu" in i else i + " mtu 1546\n"

for iface in interfaces:
    interfaceslist = iface.split("!")
    updated_ifaces = [add_mtu(i) for i in interfaceslist]

Note that I replaced the first i with iface for clarity. And also, it appears to me that there's only one iface in interfaces right now. Perhaps you need that for loop, but if not, it would simplify things to remove it.

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

3 Comments

Thanks a lot senderle, it looks awesome!! I will try it soon and let you know.. Thanks again..
senderle - It works great with MTU.. How about concerning description statement along with MTU. How do I go about integrating that side of the code? Do I have to create a different function for that? Any code will be appreciated.. I have cases where either of the statements "mtu" or "description" could be missing from the text file or both. Thanks again for all the help
You could just modify the current add_mtu function. Instead of returning the result of i if "mtu" in..., save it to a variable name and do the same thing with the description.
1

If you can read the entire file:

import re

f = open('/TESTFOLDER/TEST.txt','r')
text = f.read()

text = re.sub(r"(?m)(^interface Vlan\d+.*\n(?! description )", r"\1 description TEST\n", text)
text = re.sub(r"(?m)(^interface Vlan\d+.*\n description .+\n)(?! mtu )", r"\1 mtu 1546\n", text)

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.