in reply to Parsing output from a command

ronix,
Perhaps something like this (untested)?
#!/usr/bin/perl use strict; use warnings; open(my $cfg, '-|', 'lscfg -vl hdisk*') or die "Unable to open pipe to + lscfg: $!"; { local $/ = "\nhdisk"; while (<$fh>) { chomp; $_ = $/ . $_ if $. != 1; my ($disk) = $_ =~ /^(hdisk\w+)/; my ($serial) = $_ =~ /Serial Number\.+(\w+)/; if ($disk && $serial) { print "$disk\t$serial\n"; } else { # enhance parser to handle follow situation die $_; } } }

Cheers - L~R

Replies are listed 'Best First'.
Re^2: Parsing output from a command
by BrowserUk (Patriarch) on Mar 24, 2009 at 16:24 UTC
      BrowserUk,
      Can you explain how it will screw up?

      I have subsequently tested it and it works as I expect it to. I did find that on my AIX, the output of the command isn't exactly the same as described by the OP - specifically there are two spaces before hdisk## but after accounting for that, it works as intended. Update: The modifications I made that may make the difference (as I said, it worked as expected) were as follows:

      local $/ = "\n hdisk"; my ($disk) = $_ =~ /\s+(hdisk\w+)/;

      Cheers - L~R

        Hm. I got this output when I ran your code on the OP's data (c&p'd from the console window):

        c:\test>junk4 hdisk0 0009E05B hdisk1 P1/Z1-Ab 16 Bit LVD SCSI Disk Drive (18200 MB) Manufacturer................IBM Machine Type and Model......ST318305LC FRU Number..................09P4437 Serial Number...............0004A0D2 Part Number.................09P4436

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.