[英]How can I parse the output of lshw using Perl regexes?
我正在尝试使用此代码将lshw
输出解析为哈希,到目前为止仍然有效。
use strict;
use warnings;
my (%lshw,$key,$value);
while (<>){
s/#.*//; # no comments
s/^\s+//; # no leading whites
s/\s+$//; # no trailing whites
next unless length; # anything left?
if (/(?<key>.*?):\s+(?<value>.*)/x){
$lshw{$+{key}} = $+{value};
}
}
# remove white spaces in hash keys
for $key (keys %lshw){
$value = delete $lshw{$key};
for ($key){
s/\s+//g;
}
$lshw{$key} = $value;
}
my $logname = $lshw{'logicalname'};
print "Logical name\t $logname\n";
但是当我遇到如下配置时我很努力:
clock: 33Mhz
width: 32 bits
capacity: 1Gbit/s
configuration:autonegotiation=on broadcast=yes driver=igb driverversion=5.3.0-k duplex=full firmware=1.63, 0x800009fa ip=[REMOVED] latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s`
我正在尝试一种方法,但是没有找到如何拆分键/值的解决方案,因为它包含多个单词值,例如port=twisted pair
。 关键永远是一个单词。
谁能给我一个提示如何解决这个问题?
(感谢simbabque提供严格/警告提示)
您需要的是捕获等号后的所有字符,然后不跟随模式somekeyname=
。
#!/usr/bin/env perl
use strict;
use warnings;
my $s = q{configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.3.0-k duplex=full firmware=1.63, 0x800009fa ip=[REMOVED] latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s};
my ($key, $rest) = split /:\s*/, $s, 2;
my %params = ($rest =~ / (\w+) = ((?:. (?! \w+ = ))+) /gx);
use YAML::XS;
print Dump \%params;
输出:
---
autonegotiation: on
broadcast: yes
driver: igb
driverversion: 5.3.0-k
duplex: full
firmware: 1.63, 0x800009fa
ip: '[REMOVED]'
latency: '0'
link: yes
multicast: yes
port: twisted pair
speed: 1Gbit/s
此外,可以改善您的初始循环:
while (<>) {
next if /^#/; # skip comments
/\S/ or next; # skip blank lines
s/^\s+//;
s/\s+\z//;
# ...
}
您只需要按如下方式split
配置字符串
use strict;
use warnings 'all';
use feature 'say';
my $s = 'configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.3.0-k duplex=full firmware=1.63, 0x800009fa ip=[REMOVED] latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s';
say for split /\s+(?=[^\s=]+=)/, $s;
configuration:
autonegotiation=on
broadcast=yes
driver=igb
driverversion=5.3.0-k
duplex=full
firmware=1.63, 0x800009fa
ip=[REMOVED]
latency=0
link=yes
multicast=yes
port=twisted pair
speed=1Gbit/s
现在,您将获得一个键及其值的列表,这些键及其值已正确地除以键名称。 这应该很容易处理
Borodin的方法就是这样。
万一您想用regexp解析它,它将起作用并且将键与其值分开。
#!/usr/bin/env perl
use warnings FATAL => 'all';
use strict;
my $s = 'configuration: autonegotiation=on broadcast=yes driver=igb driverversion=5.3.0-k duplex=full firmware=1.63, 0x800009fa ip=[REMOVED] latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s';
while ($s =~ m/(?<key>[A-Za-z0-9]+)=(?<value>([\/\[\]A-Za-z0-9., -]+)(?= [a-z]+)|([\/\[\]A-Za-z0-9., -]+))/g) {
print "$+{key} >> $+{value}\n";
$s =~ s/$+{key}//;
}
输出量
autonegotiation >> on
broadcast >> yes
driver >> igb
driverversion >> 5.3.0-k
duplex >> full
firmware >> 1.63, 0x800009fa
ip >> [REMOVED]
latency >> 0
link >> yes
multicast >> yes
port >> twisted pair
speed >> 1Gbit/s
优点
缺点
重构提案
[\\/\\[\\]A-Za-z0-9., -]
的复杂字符组[\\/\\[\\]A-Za-z0-9., -]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.