http://qs321.pair.com?node_id=11108728


in reply to Parse variables from @output

Have expanded the variables further to fix more issues now.
#!/usr/local/bin/perl ## use strict; use warnings; use Data::Dumper; my @output = ( qq/Software\n/ .qq/ BIOS: version 3.6.0\n/ .qq/ kickstart: version 7.1(5)N1(1)\n/ .qq/ system: version 7.1(5)N1(1)\n/ .qq/ kickstart image file is: bootflash:\/\/\/n5000-uk9-kickstart +.7.1.5.N1.1.bin\n/ .qq/ system image file is: bootflash:\/\/\/n5000-uk9.7.1.5.N1. +1.bin\n/ .qq/Hardware\n/ .qq/ cisco Nexus 5548 Chassis ("O2 32X10GE\/Modular Universal Pla +tform Supervisor")\n/ .qq/ Intel(R) Xeon(R) CPU with 8253840 kB of memory.\n/ .qq/ Processor Board ID FOC163849D1\n/ .qq/ Device name: tpecasw01\n/ .qq/ bootflash: 2007040 kB\n/ .qq/Kernel uptime is 636 day(s), 1 hour(s), 34 minute(s), 49 secon +d(s)\n/ ); print "@output\n"; foreach (@output) { my %details; if (/^Hardware/mi) { $details{'Chassis'} = $1 if (/^\s*cisco Nexus (\S+) Chassi +s/mi); $details{'Main Memory'} = $1 if (/^\s*Intel.* with (\S+) kB of + memory/mi); $details{'Name'} = $1 if (/^\s*Device name: (\S+)/mi); $details{'Uptime'} = $1 if (/^\s*Kernel uptime is (.*?)\s*$/mi +); $details{'Version'} = $1 if (/^\s*system: .*version (\S+?)[,\s +]/mi); $details{'IOS File'} = $1 if (/^\s*system image file is:\s*"(. +*)"/mi); $details{Serial} = $1 if (/^\s*Processor board ID (\S+)/mi); $details{'ROM Version'} = $1 if (/^\s*BIOS: .*version (\S+?)[, +\s]/mi); # aka BIOS $details{'Boot Version'} = $1 if (/^\s*kickstart: .*version (\ +S+?)[,\s]/mi); # aka kickstarter $details{'Boot Flash'} = $1 if (/^\s*bootflash: (\S+)/mi); } print Dumper(\%details); }

All are working apart from $details('IOS File') is not populating/displaying. Any ideas? Result should be bootflash:///n5000-uk9.7.1.5.N1.1.bin

Value for 'IOS File' missing in results...

Software BIOS: version 3.6.0 kickstart: version 7.1(5)N1(1) system: version 7.1(5)N1(1) kickstart image file is: bootflash:///n5000-uk9-kickstart.7.1.5.N1.1 +.bin system image file is: bootflash:///n5000-uk9.7.1.5.N1.1.bin Hardware cisco Nexus 5548 Chassis ("O2 32X10GE/Modular Universal Platform Sup +ervisor") Intel(R) Xeon(R) CPU with 8253840 kB of memory. Processor Board ID FOC163849D1 Device name: tpecasw01 bootflash: 2007040 kB Kernel uptime is 636 day(s), 1 hour(s), 34 minute(s), 49 second(s) $VAR1 = { 'Uptime' => '636 day(s), 1 hour(s), 34 minute(s), 49 second( +s)', 'Serial' => 'FOC163849D1', 'Version' => '7.1(5)N1(1)', 'ROM Version' => '3.6.0', 'Main Memory' => '8253840', 'Name' => 'tpecasw01', 'Boot Version' => '7.1(5)N1(1)', 'Chassis' => '5548' };

Replies are listed 'Best First'.
Re^2: Parse variables from @output
by Corion (Patriarch) on Nov 15, 2019 at 15:44 UTC

    Again, compare your regular expression against the string you're trying to match, and eliminate the differences:

    system image file is: bootflash:\/\/\/n5000-uk9.7.1.5.N1.1.bin +\n /^\s*system image file is:\s*"(.*)"/mi

    Maybe you want to use a more interactive tool to find where your regex fails, for example haukex's interactive Perl regex tester.

      Already tried that one, doesn't work I'm afraid

        Which one did you try? I didn't post any solution. I only lined up your input string with your regular expression to make it easier for you to see the differences.

        If you look closely, you might find that the regex includes double quotes while your string does not. What do the double quotes mean there?

        Sorry, with you now. Taken the quotes out, ok now just a few more to sort. Thanks again.