I'm looking for some assistance on a script which can go through the lines of an array, printing them to the screen and stopping when the script detects a specific character, in this case the !
mark. I have tried using the foreach
statement but ain't getting any success...
Example of the Array (@lines
) contents is:
ip vrf test
rd 2856:10000331
export map SetAltMgmtRT
route-target export 2856:10000331
route-target import 2856:10000331
maximum routes 1000 75
!
The script I have so far is:
elsif ( $action eq "show_vrf" ) {
my $cmd = "show run | begin <VRF_NAME>";
$cmd = $cmd . " | i $include" if($include) ;
my @lines = $s->cmd(String => $cmd,
Prompt => "/$enableprompt/",
Timeout => 10);
foreach $lines (@lines) {
<statement, this is where I am stuck>
}
print $lines;
Any help would be appreciated :)
What is the criteria for stopping? Any exclamation mark or just one on its own line? Or just a line beginning with an exclamation mark?
You've also got several things called lines
which you need to sort out.
my $output;
foreach my $line (@lines) {
last if $line =~ m/^!/; # leave loop if line starts with an exclamation mark
$output .= $line;
}
print $output;
For your additional requirements in your comment below (the data has more than one exclamation mark) you'd need something like this:
use Data::Dumper;
my @output; # assign output chunks into an array
my $i = 0;
foreach my $line (@lines) {
if ($line =~ m/^!/) {
$i++;
next;
}
$output[$i] .= $line;
}
print Dumper(\@output);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加