I have a Cisco config file and I want to parse all vrf information, route-targets, policies etc. The problem I have is parsing multiple lines of route-targets as they do not have an associated descriptor in the same line, only white space. The number of route-targets can vary. One vrf might have two import RT and one export RT, next vrf slightly different amount of entries. I want to keep import RT and export RT separate so the rule should be "for vrf A, give me the import route targets and then give me the export route targets". It should stop after the import RT have been collected, meaning it doesn't run into the next section and collect something by accident. Then after that it should collect the export RT values. I have varying results, I usually always get the last route-target entry. I tried a variation of this as well and just got the last RT. How to parse text over multiple lines with textfsm?
This is my input. I want to know the import and export route targets for vrf myvrf, and do this with all vrfs. In this case it's importRT 111:500 and 222:500. Then export RT 333:1000. As mentioned, each vrf might have a different number of RT so the rule has to stop after import and stop after export but collect each value for import and export.
vrf myvrf
address-family ipv4 unicast
import route-policy import_rules
import route-target
111:500
222:500
!
export route-policy export_rules
export route-target
333:1000
!
Value VRF (\S+)
Value importRT (\S+[\S+]+)
Value importRT2 (\S+[\S+]+)
Start
^vrf ${VRF}
^\s+${importRT}
^\s+${importRT2}
^\s+! -> Record
Current result
{
"VRF": "myvrf",
"importRT": "222:500",
"importRT2": ""
}