2

I apologize if this post is long, but I am trying to be as detailed as possible. I have done a considerable amount of research on the topic, and would consider myself an "intermediate" skilled programmer.

My problem: I have a text file with multiple lines of data. I would like to remove certain parts of each line in an effort to get rid of some irrelevant information, and then save the file with the newly formatted lines.

Here is an example of what I am trying to accomplish. The original line is something like:

access-list inbound_outside1 line 165 extended permit tcp any host 209.143.156.200 eq www (hitcnt=10086645) 0x3eb90594

I am trying to have the code read the text file, and output:

permit tcp any 209.143.156.200 www 

The following code works, but only if there is a single line in the text file:

input_file = open("ConfigInput.txt", "r")
output_file = open("ConfigOutput.txt", "w")

for line in input_file:
    line = line.split("extended ", 1)[1]
    line = line.split("(", 1)[0]
    line = line.replace(" host", "")
    line = line.replace(" eq", "")
    output_file.write(line)

output_file.close()
input_file.close()

However, when I attempt to run this with a full file of multiple lines of data, I receive an error:

File "C:\Python27\asaReader", line 5, in <module>
    line = line.split("extended ", 1)[1]
IndexError: list index out of range

I suspect that it is not moving onto the next line of data in the text file, and therefore there isn't anything in [1] of the previous string. I would appreciate any help I can get on this.

3 Answers3

2

Some possible causes:

  • You have blank lines in your file (blank lines obviously won't contain the word extended)
  • You have lines that aren't blank, but don't contain the word extended

You could try printing your lines individually to see where the problem occurs:

for line in input_file:
    print("Got line: %s" % (line))
    line = line.split("extended ", 1)[1]

Oh, and it's possible that the last line is blank and it's failing on that. It would be easy enough to miss.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • Each line contains the word "extended" and there are no blank lines in the text file. It has very consistent formatting. – user1650583 Sep 06 '12 at 01:44
  • @user1650583 Could you try adding the print statement I just edited in and let us know which line is causing the error? – Brendan Long Sep 06 '12 at 01:45
  • I have just gone through with a search function for line breaks, and found one at the end of my file. This has solved the issue (and I am verbally praising you as I write this). I did not know that would throw an error, but either way, you have fixed my issue. Thank you so much for your quick response, as I can now continue with my night! – user1650583 Sep 06 '12 at 01:47
  • @Levon waiting for the remaining 2 minutes to accept! Thanks again for such quick responses. I learned a lot of coding from this site and appreciate all the help. – user1650583 Sep 06 '12 at 01:51
  • @user1650583 ah .. ok. Agreed on SO, an incredible resource (and most people manage to help and be nice about it :) – Levon Sep 06 '12 at 01:51
1

Print something out when you hit a line that can't be processed

for line in input_file:
    try:
        line = line.split("extended ", 1)[1]
        line = line.split("(", 1)[0]
        line = line.replace(" host", "")
        line = line.replace(" eq", "")
        output_file.write(line)
    except Exception, e:
        print "Choked on this line: %r"%line
        print e
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
0

An alternate approach would be to cache all the lines (assuming the file is not humongous.)

>>> with open('/tmp/ConfigInput.txt', 'rU') as f:
...     lines = f.readlines()
...     
... 
>>> lines
['access-list inbound_outside1 line 165 extended permit tcp any host 209.143.156.200 eq www (hitcnt=10086645) 0x3eb90594\n']
>>> lines = [re.sub('(^.*extended |\(.*$)', '', line) for line in lines]
>>> lines
['permit tcp any host 209.143.156.200 eq www \n']
>>> with open('/tmp/ConfigOutput.txt', 'w') as f:
...     f.writelines(lines)
...     
... 
>>> 
Rg Glpj
  • 423
  • 2
  • 5
  • 10