i want to extract specific data from txt file like 'Port-channel2''vlan'column and put what is after vlan in this column. this is part of the txt file:
VLAN + IP address --------------------23 Interface IP-Address OK? Method Status Protocol Vlan1 unassigned YES unset up up
Vlan5 10.26.95.33 YES NVRAM up up
Vlan20 10.26.93.1 YES NVRAM up up
Vlan30 10.26.93.65 YES NVRAM up up
Vlan40 10.63.121.251 YES NVRAM down down
Vlan50 10.50.50.54 YES NVRAM up up
Vlan60 10.26.95.22 YES NVRAM down down
Vlan101 10.26.92.1 YES NVRAM up up
Vlan131 10.26.81.1 YES NVRAM down down
Vlan134 10.26.82.1 YES NVRAM up up
Vlan151 10.26.83.1 YES NVRAM down down
Vlan201 10.26.80.1 YES NVRAM up up
Subnet Mask -------------39 Internet address is 10.210.130.10/30 Internet address is 172.16.1.202/24 Internet address is 151.151.151.151/32 Internet address is 10.26.95.33/27 Internet address is 10.26.93.1/26 Internet address is 10.26.93.65/26 Internet address is 10.63.121.251/28 Internet address is 10.50.50.54/24 Internet address is 10.26.95.22/29 Internet address is 10.26.92.1/24 Internet address is 10.26.81.1/24 Internet address is 10.26.82.1/24 Internet address is 10.26.83.1/24 Internet address is 10.26.80.1/24 ##################################################################################
LAN Sheet55 ############
Access or Trunk with VLANs
interface Port-channel2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface Port-channel1 interface GigabitEthernet0/2 switchport trunk allowed vlan 5,20,30,101,134,201,381 interface GigabitEthernet0/3 interface GigabitEthernet0/0 interface GigabitEthernet0/1 channel-group 1 mode on interface GigabitEthernet1/0 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on70 interface GigabitEthernet1/1 switchport trunk allowed vlan 5,20,30,101,134,201,381 channel-group 2 mode on interface GigabitEthernet1/2 interface GigabitEthernet1/3 ip route 172.16.1.203 255.255.255.255 GigabitEthernet0/1 the expected output is expected output
Here is one of many possible ways, but the shortest way (and probably least robust) way I could think of, given the text provided in the question:
def extract_interface_info(config_text):
"""Returns a list of tuples:
[(port id, channel-group, switchport mode,vlans),...]
"""
return [ (interface[0], # port id
interface[interface.index('channel-group')+1]
if 'channel-group' in interface else '',
interface[interface.index('switchport')+1],
interface[interface.index('vlan')+1]
)
for interface in
( interface_str.split()
for interface_str in config_text.split('interface')
if 'switchport' in interface_str and 'vlan' in interface_str
)
]
info = extract_interface_info(text)
print('\n'.join(str(interface) for interface in info))
prints:
('Port-channel2', '', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet0/2', '', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet1/0', '2', 'trunk', '5,20,30,101,134,201,381')
('GigabitEthernet1/1', '2', 'trunk', '5,20,30,101,134,201,381')
I used a generator that splits out the text by the keyword "interface" and filtered the resulting strings by keywords required to appear in the desired sections: 'switchport' and 'vlan'. Each string meeting the criteria is split by whitespace and fed to the list comprehension statement
The list comprehension statement then puts the desired items from the string into a tuple for each interface and returns a list of those tuples.
Note: channel-group does not appear in all interfaces, so a conditional statement is needed to only look up the index of 'channel-group' if it exists, otherwise, a null str is returned.
Further Note: since this is only a snippet of the file, if 'switchport' and 'vlan' were to occur outside the desired context, this solution would fail in some way, such as including one or more erroneous tuples or raising an IndexError.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.