[英]Python script to Grep strings from input file and output in csv format
I am writing a python script to grep string from file and display output in csv file in below format 我正在写一个python脚本到文件中的grep字符串并以以下格式在csv文件中显示输出
enter image description here 在此处输入图片说明
enter image description here 在此处输入图片说明
Input file(result_EPFT_config_device) : 输入文件(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. 以下是我目前准备的python脚本。 Only able to grep string and print it
仅能grep字符串并打印
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 现在在这里我想把输出放在csv文件中,如下所示
enter image description here 在此处输入图片说明
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. 如上面的屏幕快照所示,如果lpt spunt超出流量陷阱,则必须将其标记为“是”(如果输入文件中存在),否则标记为“否”。 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 您能帮我实现上述要求以csv格式输出的要求吗
Here we go! 开始了! So as the comment above said you can just use "startswith" rather then regex to match the lines.
因此,正如上面的评论所述,您可以只使用“ startswith”,然后使用正则表达式来匹配行。
I have used python3 rather then python2 here. 我在这里使用了python3而不是python2。
If you run this in a directory using "python3 main.py" it will search the "inputs" subdirectory for all files to parse. 如果使用“ python3 main.py”在目录中运行此文件,它将在“ inputs”子目录中搜索所有要分析的文件。
We then build a dictionary for each file with the relevant fields and load their values. 然后,我们使用相关字段为每个文件构建一个字典,并加载它们的值。 We add these dictionaries to a list.
我们将这些字典添加到列表中。 At the end we just write the headers to the csv then loop through the rows and write values.
最后,我们只将标头写入csv,然后遍历各行并写入值。 You could probably write the rows while reading the files but I find separating parsing and output cleaner mentally.
您可能在读取文件时写了行,但是我发现从精神上分离了解析和输出清理器。
I changed the ordering of your "for lines in line" to "for line in lines" as you want to loop through each line in the lines. 当您要遍历行中的每一行时,我将“行中的行”的顺序更改为“行中的行”。
import os
import csv
def parseFile(fileName):
# We are using a dictionary to store info for each file
data = dict()
# Set all Yes/Nos to NO by default
data["lpts punt excessive-flow-trap"] = "NO"
data["dampening"] = "NO"
data["non-subscriber-interfaces"] = "NO"
fh = open(fileName, "r")
lines = fh.readlines()
for line in lines:
# We need this so we don't end up with newline characters in our CSV
line = line.rstrip("\n")
# We dont need regular expressions here as matching whole line
# Do YES/NO first
if line == "lpts punt excessive-flow-trap":
data["lpts punt excessive-flow-trap"] = "YES"
continue;
if line == "dampening":
data["dampening."] = "YES"
continue;
if line == "non-subscriber-interfaces":
data["non-subscriber-interfaces"] = "YES"
continue;
# Now do the rest
if line.startswith("Hostname"):
splitted = line.split(' ')
data["Hostname"] = splitted[1]
continue;
if line.startswith("loopback"):
splitted = line.split(' ')
data["loopback"] = splitted[1]
continue;
if line.startswith("penalty-rate arp"):
print("ARP")
splitted = line.split(' ')
data["penalty-rate arp"] = splitted[2]
continue;
if line.startswith("penalty-rate icmp"):
splitted = line.split(' ')
data["penalty-rate icmp"] = splitted[2]
continue;
if line.startswith("penalty-rate igmp"):
splitted = line.split(' ')
data["penalty-rate igmp"] = splitted[2]
continue;
if line.startswith("penalty-rate ip"):
splitted = line.split(' ')
data["penalty-rate ip"] = splitted[2]
continue;
if line.startswith("report-threshold"):
splitted = line.split(' ')
data["report-threshold"] = splitted[1]
continue;
return data
if __name__ == "__main__":
inputsDirectory = "inputs"
path = os.path.abspath(inputsDirectory)
fileList = ["{}/{}".format(path,x) for x in os.listdir(inputsDirectory)]
print(fileList)
# Load Each File and Build Dictionary
csvRows = []
for file in fileList:
newRow = parseFile(file)
csvRows.append(newRow)
print(csvRows)
# Output CSV using dictionaries for each file
outputFile = "output.csv"
with open(outputFile, 'w') as csvfile:
fieldnames = ["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"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in csvRows:
writer.writerow(row)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.