regexperlzfs

Regex string extraction in Perl Script


my text from a cmd output is like this:

  pool: pool0
 state: ONLINE
status: Some supported features are not enabled on the pool. The pool can
        still be used, but some features are unavailable.
action: Enable all features using 'zpool upgrade'. Once this is done,
        the pool may no longer be accessible by software that does not support
        the features. See zpool-features(5) for details.
  scan: scrub repaired 0B in 01:24:15 with 0 errors on Sun Nov 14 01:48:17 2021
config:

        NAME        STATE     READ WRITE CKSUM
        pool0       ONLINE       0     0     0
          mirror-0  ONLINE       0     0     0
            sda     ONLINE       0     0     0
            sdb     ONLINE       0     0     0
          mirror-1  ONLINE       0     0     0
            sdc     ONLINE       0     0     0
            sdd     ONLINE       0     0     0

errors: No known data errors

I would like to extract the values for pool, state, status, action, scan, config and errors. I tryíed to use a regex gen/test side like https://regexr.com/ or https://regex101.com/ . There i get my matches and the value in the "name" group for the pool example but in shell i can see nothing

use IO::Socket::INET;
my $strCmdErg = `/sbin/zpool status`;

my $poolname=  $strCmdErg=~ /pool:\s(.*$)/gm;
print "PoolName: $poolname \n";

my $state = $strCmdErg=~ /\sstate:\s(.*$)\n/gm;
print "Status: $state \n";

output

PoolName: 1
Status: 1

I think the "one" is an indicator for a match.

Thank you!


Solution

  • my $poolname=
    

    Is doing the regex in scalar context, which is why you get 1, the number of matches, returned. You need to change it to list context like

     my ($poolname) =
    

    in order for the captured text to be assigned to the variable.