http://www.perlmonks.org?node_id=1208837


in reply to Store multiple lines

#!/usr/bin/perl # http://perlmonks.org/?node_id=1208816 use strict; use warnings; local $_ = do { local $/; <DATA> }; my %results; # match two lines at a time $results{$1}{$5}{$2}{$3} = $4 while /^(\S+):\s+ static-route\s(\S+)\s next-hop\s(\S+) (?:\spreference\s\d+)? (?:\sbfd-enable)?\s tag\s(\d ++) \n \1-\s* service-name\s"(\S+)"/gmx; for my $box (sort keys %results) { print "\n$box\n"; for my $service (sort keys %{$results{$box}}) { for my $static (sort keys %{$results{$box}{$service}}) { for my $hop (sort keys %{$results{$box}{$service}{$static}}) { print "$static\t next-hop $hop\t", "tag $results{$box}{$service}{$static}{$hop}, service $servi +ce\n"; } } } } __DATA__ dnsname1: static-route 1.1.1.1/30 next-hop 2.2.2.2 preferen +ce 200 bfd-enable tag 685 dnsname1- service-name "XYZ12345-s12345" dnsname2: static-route 3.3.3.3/30 next-hop 4.4.4.4 bfd-enab +le tag 635 dnsname2- service-name "XYZ67891-s67890"

Outputs:

dnsname1 1.1.1.1/30 next-hop 2.2.2.2 tag 685, service XYZ12345-s123 +45 dnsname2 3.3.3.3/30 next-hop 4.4.4.4 tag 635, service XYZ67891-s678 +90