bartrad has asked for the wisdom of the Perl Monks concerning the following question:
Hi all, after some advice. I'm opening a file and reading through it and look for some key values after the "Count:" text, but my next if's don't seem to be working properly (as they're probably not doing what I think they're doing) and instead it seems to be ignoring the next ifs and just picking up every occurrence of "Count:"
The input file
Checks: Running 'show port | match "Up Yes" | count' Count: 23 lines Checks: Running 'show router interface | match "Up " | count' Count: 4 lines
My Code
open( INPUT, "$folder/$file" ) or die("Could not open $file file"); while ( my $line = <INPUT> ) { next if ( $line =~ m/show port/ ); if ( $line =~ m/Count:\s+(\d+)/ ) { $checks{port}{$stage} = $1; } next if ( $line =~ m/show router interface/ ); if ( $line =~ m/Count:\s+(\d+)/ ) { $checks{l3}{$stage} = $1; } }
Printing out $checks{port}{$stage} shows:
23 4
When it should only show 23 for $checks{port}{$stage} and 4 for $checks{l3}{$stage}...
And a Dumper
$VAR1 = { 'card' => { 'post' => '28615', 'pre' => '28615' }, 'l3' => { 'post' => '28615', 'pre' => '28615' }, 'subs' => { 'post' => '2', 'pre' => '2' }, 'bfd' => { 'post' => 0, 'pre' => 0 }, 'binding' => { 'post' => '28615', 'pre' => '28615' }, 'ospf' => { 'post' => '2', 'pre' => '2' }, 'port' => { 'post' => '28615', 'pre' => '28615' }, 'ldp' => { 'post' => '14', 'pre' => '14' } };
Note, there's other occurrences of "Count" it's picking up but I only included two from the data source to keep things short. Any help is greatly appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Iterating and pattern matching on input file
by roboticus (Chancellor) on Jul 31, 2018 at 20:06 UTC | |
by AnomalousMonk (Archbishop) on Jul 31, 2018 at 20:50 UTC | |
by bartrad (Beadle) on Jul 31, 2018 at 20:41 UTC | |
by roboticus (Chancellor) on Jul 31, 2018 at 23:50 UTC | |
by AnomalousMonk (Archbishop) on Jul 31, 2018 at 23:57 UTC |