#!/usr/bin/perl # http://perlmonks.org/?node_id=1208816 use strict; use warnings; local $_ = do { local $/; }; 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 $service\n"; } } } } __DATA__ dnsname1: static-route 1.1.1.1/30 next-hop 2.2.2.2 preference 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-enable tag 635 dnsname2- service-name "XYZ67891-s67890"