in reply to Trying to split a file into array conditionally
/me is with hbm on "INTERFACENAME." The logic of the question, it seems to me, requires that "INTERFACENAME" be OP's standin for variable text. And on that interpretation, bart's answer is elegant after one coerces the data into a single string. ++
So, TIMTOWTDI. I've assumed variable text (and that the variable text ends with an uppercase alpha, [A-Z] character).
#!/usr/bin/perl use Modern::Perl; # 938143 my $cachedline=""; my $multiline=""; my (@data, $line, @array_of_routeinfo); my $re = qr/directly connected, /; while ( <DATA> ) { # slurp __DATA__ push @data,$_ ; } for $line(@data) { chomp $line; $line =~ s/ $//g; # clean up data; one of mine had trailing s +paces if ($line eq '') { say "No more data"; exit(1); } if ( ($line =~ /$re/ && $cachedline eq "") ) { push @array_of_routeinfo, $line; } elsif ( ($line !~ /$re/) && ($line =~ /\d$/) ) { # if $line do +esn't contain "directly connected, " and ends with number $cachedline= $line; } elsif ( ($line !~ /re/ && $cachedline ne "") ) { # no "directly + connected, " AND $cachedline has content $multiline = "\t $cachedline"; $multiline .= "\n\t $line"; push @array_of_routeinfo, $multiline; $cachedline = ""; } } for (@array_of_routeinfo) { say; } say "Done"; __DATA__ C AAA.BBB.CCC.DDD 255.255.255.224 is directly connected, FOO O E1 WWW.XXX.YYY.ZZZ 255.255.224.0 [110/112] via AAA.BBB.DDD.EEE, 696:56:46, BAR B 111.222.333.444 255.255.255.0 [914/212] via 111.222.333.444, 345:59:03, BAZ X EEE.FFF.GGG.HHH 255.255.255.123 is directly connected, BAT 3 L1 OOO.PPP.QQQ.RRR 255.255.111.77 [A10/304/0xDFEE] via 444.333.222.000, 504:27:88, BLIVITZ
Output is:
C AAA.BBB.CCC.DDD 255.255.255.224 is directly connected, FOO O E1 WWW.XXX.YYY.ZZZ 255.255.224.0 [110/112] via AAA.BBB.DDD.EEE, 696:56:46, BAR B 111.222.333.444 255.255.255.0 [914/212] via 111.222.333.444, 345:59:03, BAZ X EEE.FFF.GGG.HHH 255.255.255.123 is directly connected, BAT 3 L1 OOO.PPP.QQQ.RRR 255.255.111.77 [A10/304/0xDFEE] via 444.333.222.000, 504:27:88, BLIVITZ Done
|
|---|