zonevbkr has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks.... I'm new here and though I've worked with Perl for a bit, still know very little.... I've inherited some code to use with an app and so can't really change that, but need to add some code to it to parse the data and pull out "a line" from the string. What I am trying to find is a way to pull out "SystemImage: flash:c3750-advipservicesk9-mz.122-46.SE.bin". (or whatever will come after SystemImage and before the end of the line. It'll always start with SystemImage. I've looked for a way to use \bSystemImage.*\b and store it in another variable, but I've yet to find a way to get that to work. It looks like \b is just used for getting a yes | no answer if something exists. I know it exists, I need to pull it out | get rid of the rest. I welcome any help!

use lib qw(blib lib); #use strict; use warnings; use Getopt::Long; use Pod::Usage; use Data::Dumper; use Opsware::NAS::Client; my($user,$pass,$host) = ('jon', 'doe', 'localhost'); my $help = 0; my $ip = '127.0.0.1'; my $nas = Opsware::NAS::Client->new(); my $res = $nas->login(-user => $user, -pass => $pass, -host => $host, +-nosession => '0'); my res = $nas->show_deviceinfo(ip => $ip)); #here's where the magic happens.... foreach my $tftp ($res->result()) { print "$tftp\n"; } $nas->logout(); Here's the output I get from the above code: DeviceInformation: Contact: NOC 111-222-3333 Hostname: bob Location: Creek Manufacturer: Cisco Model: WS-C3750G-48TS-S (C3750 series) OSVer: 12.2(46)SE Processor: WS-C3750G-48TS (PowerPC405) ROMVer: 12.2(25r)SEE4 SerialNumber: Z4CF,FOC688 ServiceTypes: Power SystemImage: flash:c3750-advipservicesk9-mz.122-46.SE.bin SystemMemory: 134209536

Replies are listed 'Best First'.
Re: problems parsing data
by graff (Chancellor) on Jul 09, 2010 at 01:49 UTC
    Since I don't know anything about the "Opsware::NAS::Client" module or the "$nas" or "$res" objects you're using, I have to guess that the $res->result() thing returns a list of lines. If that's true, then you probably want the "grep" function rather than a "foreach" loop:
    print grep /^SystemImage: /, $res->result(); # replaces your "foreach +" block
    If there are two or more "SystemImage" lines in the list being returned, you'll see all of them printed with that approach.
Re: problems parsing data
by ikegami (Patriarch) on Jul 09, 2010 at 00:55 UTC
    my $tftp = <<'__EOI__'; DeviceInformation: Contact: NOC 111-222-3333 Hostname: bob Location: Creek Manufacturer: Cisco Model: WS-C3750G-48TS-S (C3750 series) OSVer: 12.2(46)SE Processor: WS-C3750G-48TS (PowerPC405) ROMVer: 12.2(25r)SEE4 SerialNumber: Z4CF,FOC688 ServiceTypes: Power SystemImage: flash:c3750-advipservicesk9-mz.122-46.SE.bin SystemMemory: 134209536 __EOI__ my ($image) = $tftp =~ /^SystemImage:\s*([^\n]+)/m or die("No SystemImage\n"); print("$image\n"); # flash:c3750-advipservicesk9-mz.122-46.SE.bin

      Thanks for the quick reply. I'll try this out. Do have a question though, maybe there's no way to do it, but is there a way I can just pass the data from

      $res->result()

      Since the data will change from device to device, just trying to find a way to make it as simple as possible. Thanks again, Chris

        ah silly me, I just clued in that $res->result() returns a list of lines. Ignore my earlier posts.
        my ($image) = map { /^SystemImage:\s*([^\n]+)/ ? $1 : () } $res->resul +t();
        Yes, the left hand side of =~ can be any expression, not just a variable.