#!/usr/bin/perl -w use strict; my @routes; my $temp=''; while() { next if /^\s*$/; #skip blank lines chomp; if (/directly connected/) { push @routes, $_; next; } if (/via/) { push @routes, "$temp$_"; $temp=''; next; } $temp .= $_; } print join("\n",@routes),"\n"; __DATA__ C AAA.BBB.CCC.DDD 255.255.255.224 is directly connected, INTERFACENAME O E1 WWW.XXX.YYY.ZZZ 255.255.224.0 110/112 via AAA.BBB.DDD.EEE, 696:56:46, INTERFACENAME