Sample Header Ad - 728x90

How to split a file based on context?

2 votes
2 answers
831 views
I have some files that contain the results of the lldpneighbors command from all our servers. I would like to split these files into individual files for each server in order to make it easier to import this data into our inventory system. **Sample Input** === Output from 00000000-0000-0000-0000-000000000000 (SERVERNAME1): Interface 'ixgbe0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/6 Time To Live: 120 seconds System Name: name-of-switch-01 End Of LLDPDU: Interface 'igb0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/23 Time To Live: 120 seconds System Name: name-of-switch-02 End Of LLDPDU: === Output from 00000000-0000-0000-0000-000000000000 (SERVERNAME2): Interface 'ixgbe0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/2 Time To Live: 120 seconds System Name: name-of-switch-01 End Of LLDPDU: Interface 'igb0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/19 Time To Live: 120 seconds System Name: name-of-switch-02 End Of LLDPDU: This is roughly what all the results look like with some variation(They are not all the same length, some are several lines longer because of more interfaces). The delimiting string I would like to match on is: === Output from [UUID] ([HOSTNAME]): Ideally I would like each file to be named the hostname(this would just be convenience and is not necessary), so above results would be split into files like: **SERVERNAME1** === Output from 00000000-0000-0000-0000-000000000000 (SERVERNAME1): Interface 'ixgbe0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/6 Time To Live: 120 seconds System Name: name-of-switch-01 End Of LLDPDU: Interface 'igb0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/23 Time To Live: 120 seconds System Name: name-of-switch-02 End Of LLDPDU: **SERVERNAME2** === Output from 00000000-0000-0000-0000-000000000000 (SERVERNAME2): Interface 'ixgbe0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/2 Time To Live: 120 seconds System Name: name-of-switch-01 End Of LLDPDU: Interface 'igb0' has 1 LLDP Neighbors: Neighbor 1: Chassis ID: MAC Address - 00 01 02 03 04 05 Port ID: Interface Name - TenGigabitEthernet 0/19 Time To Live: 120 seconds System Name: name-of-switch-02 End Of LLDPDU: I'm trying to use csplit to accomplish this but I'm not able to match the regex for some reason. The commands I've tried: $ csplit jbutryn_us-west-a_neighbors %===.*:% '{20}' csplit: ===.*:: no match $ csplit jbutryn_us-west-a_neighbors /===.*:/ '{20}' 552 552 552 csplit: ===.*:: no match $ csplit jbutryn_us-west-a_neighbors '/===.*:/' '{20}' 552 552 552 csplit: ===.*:: no match $ csplit -ks -f test jbutryn_us-west-a_neighbors '/===.*:/' '{20}' csplit: ===.*:: no match Any suggestions?
Asked by jesse_b (41447 rep)
Aug 24, 2017, 03:44 PM
Last activity: Aug 24, 2017, 04:41 PM