in reply to Grabbing Structured multi line output from a file?

When I find myself wanting to deal with structured text in multi-line chunks, I usually find the $/ variable to be helpful ( look for $INPUT_RECORD_SEPARATOR in perlvar). If you can come up with a text string that defines the boundaries between the chunks, you can read them in and deal with them individually. With a small file, you could slurp in the whole thing and split it with a regex, but that runs into trouble with larger files.

So, something like this will give you your IPs and their interface names as a hash:

#!/usr/bin/env perl use 5.010; use strict; use warnings; use Data::Printer; my %ints; local $/ = "next\n edit"; # break lines on this instead of newline while(my $chunk = <DATA>){ if( $chunk =~ /"(\w+)".*set ip (\d+\.\d+\.\d+\.\d+) /s ){ $ints{$1} = $2; } } say p %ints; __DATA__ config system interface edit "dmz" set vdom "root" set ip 192.18.254.1 255.255.254.0 set allowaccess ping fgfm set type physical set alias "Guest-Wlan" next edit "wan2" set vdom "test" set ip 10.4.254.198 255.255.255.252 set allowaccess ping https ssh snmp set type physical set alias "To MON ospf int" next edit "wan1" set vdom "root" set ip 1.1.1.1 255.255.255.252 set allowaccess ping https set type physical set alias "To Internet" next edit "modem" set vdom "root" set mode pppoe set allowaccess fgfm set type physical next edit "ssl.root" set vdom "root" set type tunnel next edit "ssl.MyRealm" set vdom "MyRealm" set type tunnel next edit "internal1" set vdom "root" set ip 10.1.16.3 255.255.255.0 set allowaccess ping https ssh snmp fgfm set type physical set alias "To MON Internal " next edit "internal2" set vdom "MyRealm" set dhcp-relay-service enable set dhcp-relay-ip "192.168.1.1" set ip 192.168.2.1 255.255.254.0 set allowaccess ping https ssh snmp fgfm set type physical set alias "To MINE" next edit "VPN-Dial" set vdom "root" set type tunnel set interface "wan1" next end

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.