【发布时间】:2021-12-26 18:04:15
【问题描述】:
我的 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/ 或 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
我认为“一”是匹配的指标。
谢谢!
【问题讨论】:
-
尝试从您的正则表达式中同时删除
$和\n,例如my $poolname= $strCmdErg=~ /pool:\s*(.*)/g;. -
提示:去掉两个匹配项上的
g。它们没有意义,而且可能有害 -
use IO::Socket::INET在您的代码示例中似乎未使用。此外,当您有另一个名为“status”的字段时,您可以为一个名为“state”的字段打印“status”。这势必会引起混乱。