DT22
DT22

Reputation: 61

Print multiple lines from text file

If i have a text file like this:

FastEthernet3/1
ip address 0.0.0.0
enable portfast

FastEthernet3/2
ip address 0.0.0.0
enable portfast

FastEthernet3/3
ip address 0.0.0.0

FastEthernet3/4
ip address 0.0.0.0

And i would like to print out the interfaces that has not enabled portfast. How do i print this in python?

i have the following code:

import os
import sys

root = "path to text file like the example above"

os.chdir(root)

current2 = os.getcwd()

print ("CWD = ", current2,"\n")


file = sys.argv[1]

f = open(file)
contents = f.read()
f.close()
print ("Number of GigabitEthernet:",contents.count("interface GigabitEthernet"))
print ("Number of FastEthernet:",contents.count("FastEthernet"))


x = open(file)
string1 = "enable portfast"
for line in x.readlines():
    if line.startswith(string1)
        print (line)
filehandle.close()

so i can find the line with enable portfast and print it, but i want it to print more lines so i know witch interface has portfast enabled.

Upvotes: 3

Views: 1601

Answers (6)

l4mpi
l4mpi

Reputation: 5149

If every interface definition starts with the string "FastEthernet", you could just split your contents by that string:

interfaces = contents.split("FastEthernet"):
for interface in interfaces:
    if "enable portfast" in interface:
        print "FastEthernet" + interface

Edit: Based on Alexanders solution, if there is always a blank line separating the interfaces, just declare interfaces like this:

interfaces = contents.split("\n\n")

... and change the print statement to print interface only.

Upvotes: 2

DT22
DT22

Reputation: 61

Thnx to Alexander it works now :) the only thing is my text file was a little different then the example. It looked like this:

FastEthernet3/1
ip address 0.0.0.0
enable portfast
!
FastEthernet3/2
ip address 0.0.0.0
enable portfast
!
FastEthernet3/3
ip address 0.0.0.0
!
FastEthernet3/4
ip address 0.0.0.0

So i had te replace the ! with a white space first and then it worked :)

import re
contents2 = contents.replace("!","")
pinterfaces = re.compile("\n\n").split(contents2)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print (pinterface)

Upvotes: 0

Minion91
Minion91

Reputation: 1929

There is no way to go back into the file so the best solution would be to keep track of the previous lines and print them when the portFast matches.

Edit to include solution: (Credit to Alexander)

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface

Upvotes: 2

Pablo Jomer
Pablo Jomer

Reputation: 10378

Try parsing all devices separately in to an array of dictionaries, like this:

[{"device_name":"FastEthernet/1","address":"1.0.0.0", "portfast": True}]

then you can loop over this hash and print the device name of the items that have the portfast value.

Upvotes: -1

Alexander
Alexander

Reputation: 23537

Splitting by interfaces based in the blank line separation:

import re
pinterfaces = re.compile("\r?\n\r?\n").split(contents)
# pinterfaces = ['FastEthernet3/1...', 'FastEthernet3/2...', ...]
for pinterface in pinterfaces:
  if "enable portfast" in pinterface:
    print pinterface

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast

Upvotes: 2

Harpal
Harpal

Reputation: 12587

with open('test.txt', 'r') as in_file:
    lines = in_file.readlines()

for i,v in enumerate(lines):
    #print i,v
    if v.strip() == 'enable portfast':
        print lines[i-2].strip()
        print lines[i-1].strip()
        print lines[i].strip()

This prints out:

FastEthernet3/1
ip address 0.0.0.0
enable portfast
FastEthernet3/2
ip address 0.0.0.0
enable portfast

Upvotes: 0

Related Questions