I wonder if anybody could help me with this challenge.
I have a folder with the following files:
switch01.txt
switch02.txt
Each file has the following contents:
show running-config
Building configuration...
Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin
show version
Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team
ROM: Bootstrap program is C3750 boot loader
show inventory
Interface IP-Address OK? Method Status Protocol
Vlan1 192.168.77.40 YES manual up up
GigabitEthernet1/0/1 unassigned YES unset up up
GigabitEthernet1/0/2 unassigned YES unset up up
GigabitEthernet1/0/3 unassigned YES unset up up
I want to split each file in the folder by the delimiter "show+whatever" and name each file by that delimiter. In this case, each file has 3 delimiters: show running-config, show version and show show inventory.
After splitting it should look like this:
Folder contents:
switch01$$$show running-config$$$.txt
switch01$$$show version$$$.txt
switch01$$$show inventory$$$.txt
switch02$$$show running-config$$$.txt
switch02$$$show version$$$.txt
switch02$$$show inventory$$$.txt
contents of the switch01$$$show running-config$$$.txt text file:
Building configuration...
Current configuration : 23611 bytes
!
! Last configuration change at 16:15:20 BST Tue Apr 27 2021 by admin
! NVRAM config last updated at 16:15:33 BST Tue Apr 27 2021 by admin
contents of the switch01$$$show version$$$.txt text file:
Cisco IOS Software, C3750 Software (C3750-IPSERVICESK9-M), Version 15.0(2)SE11, RELEASE SOFTWARE (fc3)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2017 by Cisco Systems, Inc.
Compiled Sat 19-Aug-17 09:28 by prod_rel_team
ROM: Bootstrap program is C3750 boot loader
contents of the switch01$$$show inventory$$$.txt text file:
Interface IP-Address OK? Method Status Protocol
Vlan1 192.168.77.40 YES manual up up
GigabitEthernet1/0/1 unassigned YES unset up up
GigabitEthernet1/0/2 unassigned YES unset up up
GigabitEthernet1/0/3 unassigned YES unset up up
and same for switch02.txt.
I pieced this code together which uses counters to uniquely name each file:
if filename.endswith(".txt"):
with open(filename,'r') as file:
output = file.read()
for command in output:
splitter = re.split('show\s(?:running-config|version|inventory)',output, flags=re.IGNORECASE)
counter=0
for item in splitter:
with open(filename.replace(".txt","")+'$$$'+str(counter)+'$$$'+'.txt', 'w') as f:
f.write("%s\n" % item)
counter = counter + 1
Which makes the files look like this:
switch01$$$0$$$.txt
switch01$$$1$$$.txt
switch01$$$2$$$.txt
switch01$$$3$$$.txt
Also at this point, switch01$$$0$$$.txt is blank and unnecessary.
What needs to happen to name each file by the delimiter used in regex?