Perl 脚本中的正则表达式字符串提取

Regex string extraction in Perl Script

我的 cmd 输出文本是这样的:

  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

我想提取池、状态、状态、操作、扫描、配置和错误的值。 我尝试使用像 https://regexr.com/ or https://regex101.com/ 这样的正则表达式 gen/test 。 在那里我得到了我的匹配项和池示例的“名称”组中的值,但在 shell 中我什么也看不到

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";

输出

PoolName: 1
Status: 1

我认为“一”是匹配的指标。

谢谢!

Perl 捕获使用变量 </code>、<code> 等:

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

您是正确的,您发布的代码中的 1 值是匹配的 return 值,即 true.

的 1

有关详细信息,请参阅 https://www.perltutorial.org/regular-expression-extracting-matches/

my $poolname=

在标量上下文中执行正则表达式,这就是返回匹配数 1 的原因。您需要将其更改为列表上下文,如

 my ($poolname) =

以便将捕获的文本分配给变量。

这种人类可读的输出可以很容易地用 split 解析成散列。

use strict;
use warnings;
use Data::Dumper;

my $data = do { local $/; <DATA> };    # slurp text into variable
my %data = grep $_,                         # remove empty fields
           map { chomp; $_ }                # remove trailing newline
           split /^\s*(\w+): */m, $data;    # split the data
print Dumper \%data;

__DATA__
  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

输出:

$VAR1 = {
          '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',
          'status' => 'Some supported features are not enabled on the pool. The pool can
        still be used, but some features are unavailable.',
          'state' => 'ONLINE',
          '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.',
          'errors' => 'No known data errors',
          'pool' => 'pool0',
          'scan' => 'scrub repaired 0B in 01:24:15 with 0 errors on Sun Nov 14 01:48:17 2021'
        };

现在您可以通过提供字段名称作为哈希键来轻松打印字段。例如:

print "PoolName: $data{pool}\n";
print "State: $data{state}\n";