【发布时间】:2018-07-16 09:33:15
【问题描述】:
我有一个文件log.txt,其中包含:
Router:94.126.126.109
Name:nl-rtm02a-ra2
show running-config interface^M
^MWed Jul 11 12:42:03.409 CET^M
! **** Configuration start **** RING rt72-central RA2 ****^M
! # RING INTERFACE CONFIGURATION^M
service-policy output NA4-PM-FRFB+COS^M
ipv4 address 84.116.244.181 255.255.255.252^M
bundle minimum-active links 1^M
load-interval 30^M
flow ipv4 monitor NA4-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
flow ipv6 monitor NA4-IPV6-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
!^M
interface Bundle-Ether1001^M
description ** ICL to RA2-SAT1 **^M
vrf NV_Mgmt^M
ipv4 point-to-point^M
ipv4 unnumbered Loopback1000^M
load-interval 30^M
flow ipv4 monitor NA4-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
flow ipv6 monitor NA4-IPV6-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
nv^M
satellite-fabric-link satellite 1001^M
remote-ports GigabitEthernet 0/0/0-43^M
!^M
!^M
!^M
interface Bundle-Ether2000^M
description ** LACP Uplink to rt53cbr68 **^M
mtu 9192^M
bundle minimum-active links 1^M
load-interval 30^M
!^M
interface Bundle-Ether2000.251^M
description ** rt53abr68 IPv4 B-Side **^M
vrf 03109128:NL_CMTS_ACCESS^M
ipv4 mtu 1500^M
ipv4 address 212.142.4.45 255.255.255.252^M
flow ipv4 monitor NA4-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
flow ipv6 monitor NA4-IPV6-MONITOR-MAP sampler NA4-SAMPLER-MAP ingress^M
encapsulation dot1q 251^M
!^M
interface Bundle-Ether2000.651^M
description ** rt53dbr68 IPv6 B-Side **^M
ipv6 nd prefix default no-autoconfig^M
ipv6 address 2a02:a200:40:56::1/64^M
encapsulation dot1q 651^M
!^M
interface Bundle-Ether2000.701 l2transport^M
description ** BSOD SDN-NFV Traffic rt53cbr68 **^M
encapsulation dot1q 2501-2699^M
在此文件中,我需要提取包含"cbr"、"abr"、"dbr" 的单词并将其存储在 CSV 文件中。
例如,在上面的内容中,我要提取:
1.rt53cbr68
2.rt53abr68
3.rt53dbr68
我尝试了以下代码:
with open("file.txt", "r") as f:
searchlines = f.readlines()
for i, line in enumerate(searchlines):
if "cbr" in line:
for l in searchlines[i:i+3]:
print l
还有一件事我想从文件内容中获取路由器值并将其存储在变量中..
【问题讨论】:
-
那么您发布的代码有什么问题?您目前只检查三件事中的一件,所以不妨看看
or。 -
fabric还包含abr。您是否希望匹配crb、abr和dbr前面和后面都有数字的单词? -
@jonrsharpe - 我想提取特定的单词而不是整个字符串..
-
所以请查看
str.split或re。给出一个minimal reproducible example,它实际上说明了具体的问题。 -
@pkpkpk - 哦,对不起,我没注意到。我只想要描述行中的那个词。
标签: python regex search extract