[英]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
我想提取池、状态、状态、操作、扫描、配置和错误的值。 我尝试使用正则表达式 gen/test 端,如https://regexr.com/或https://regex101.com/ 。 在那里我得到了我的匹配项和池示例的“名称”组中的值,但在 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
我认为“一”是匹配的指标。
谢谢!
my $poolname=
在标量上下文中执行正则表达式,这就是为什么返回 1(匹配数)的原因。 您需要将其更改为列出上下文,例如
my ($poolname) =
以便将捕获的文本分配给变量。
Perl 捕获使用变量$1
、 $2
等:
$strCmdErg =~ /pool:\s(.*$)/m;
my $poolname = $1;
print "PoolName: $poolname \n";
您发布的代码中的 1 值是匹配的返回值,这是true
,即true
为 1 。
有关更多信息,请参阅https://www.perltutorial.org/regular-expression-extracting-matches/ 。
这种人类可读的输出可以很容易地解析为带有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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.