I am writing a python script to grep string from file and display output in csv file in below format
Input file(result_EPFT_config_device) :
Hostname SIM-MPL-LTE-PE-RTR-134
loopback 22.13.7.34
lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
exclude interface Bundle-Ether6
exclude interface Bundle-Ether8
exclude interface Bundle-Ether15
exclude interface Bundle-Ether16
exclude interface Bundle-Ether53
exclude interface TenGigE0/0/1/1
exclude interface TenGigE0/1/1/0
exclude interface Bundle-Ether6.2
exclude interface Bundle-Ether6.4
exclude interface Bundle-Ether8.2
exclude interface Bundle-Ether8.4
exclude interface Bundle-Ether16.2
exclude interface Bundle-Ether16.4
exclude interface Bundle-Ether53.2
exclude interface TenGigE0/0/1/3.100
exclude interface TenGigE0/0/1/3.102
exclude interface TenGigE0/0/1/3.103
exclude interface TenGigE0/0/1/3.104
exclude interface TenGigE0/1/1/0.100
exclude interface GigabitEthernet0/0/0/1
exclude interface GigabitEthernet0/0/0/6
exclude interface GigabitEthernet0/0/0/9
dampening.
non-subscriber-interfaces
report-threshold 10
Below is the python script i have prepared as of now. Only able to grep string and print it
import sys
import telnetlib
import os
import subprocess
import re
import csv
fh = open("result_EPFT_config_device", "r")
fh1 = open("testingAjay", "w+")
line = fh.readlines()
for lines in line:
if re.search("(lpts punt excessive-flow-trap)", lines):
m = (lines.split(' '))
print m[0], m[1], m[2]
if re.search("(penalty-rate arp)", lines):
n = (lines.split(' '))
print n[0], n[1], n[2]
if re.search("(penalty-rate icmp)", lines):
a = (lines.split(' '))
print a[0], a[1], a[2]
if re.search("(penalty-rate igmp)", lines):
b = (lines.split(' '))
print b[0], b[1], b[2]
if re.search("(penalty-rate ip)", lines):
c = (lines.split(' '))
print c[0], c[1], c[2]
if re.search("(dampening)", lines):
c = (lines.split(' '))
print c[0]
if re.search("(non-subscriber-interfaces)", lines):
c = (lines.split('-'))
print c[0], c[1], c[2]
if re.search("(report-threshold 10)", lines):
c = (lines.split(' '))
print c[0], c[1]
My script output :
lpts punt excessive-flow-trap
penalty-rate arp 10
penalty-rate icmp 50
penalty-rate igmp 50
penalty-rate ip 100
dampening.
non subscriber interfaces
report-threshold 10
Now here i want to put the output in csv file as shown below
Hostname|loopback|lpts punt excessive-flow-trap|penalty-rate arp|penalty-rate icmp|penalty-rate igmp|penalty-rate ip|dampening|non-subscriber-interfaces|report-threshold
SIM-MPL-LTE-PE-RTR-134|1.1.1.1|yes|10|50|50|100|Yes|Yes|10
NDL-MPL-PE-RTR-195|2.2.2.2|No|No|No|20|50|NO|20Yes
As shown in above screenshot, column lpt spunt excessive flow trap has to mark as YES if its present in Input File else mark NO. Similar logic needs to apply for column dampening and non subscriber interface column
Could you please help me to achieve require output in csv format as shown above
Hostnameif lines.startswith('lpts punt excessive-flow-trap'):