不确定这是否可行。我需要在perl中解析一个文本文件,以提取以数千行长的IP地址开头的行。然而,需要注意的是我搜索的模式可能会在文本文件中出现数次,但我只想提取它的一个实例...下面的文本文件示例:
Random network information.
cn_DomainIntity : ps_Domain
more text
10.162.55.123 RadioBearerSetup
some text
some more text
cs_Domain
random text
10.162.55.136 RadioBearerSetupComplete
network stats
10.162.55.122 RadioBearerSetup
text
ps_Domain
text
10.162.66.125 RadioBearerSetupComplete
...
...
基本上我想要做的是提取以RadioBearerSetup开头并以RadioBearerSetupComplete结尾但在其间包含ps_Domain的文本文件的行。
我一直在使用触发器操作器,虽然它有点像我想要的,它提取文件的所有实例,以RadioBearerSetup开头并以RadioBearerSetupComplete结束 我的代码如下:
use strict;
use warnings;
my $file= "datafile.txt";
open (my $FH, "<", $file) or die "Cannot open <$file>: $!";
while ($FH) {
print if /\bRadioBearerSetup\b/ .. /\bRadioBearerSetupComplete\b/;
}
close($FH);
答案 0 :(得分:3)
我会做类似的事情:
my @data;
my $found = 0;
while(<DATA>) {
chomp;
if (/\bRadioBearerSetup\b/ .. /\bRadioBearerSetupComplete\b/) {
$found = 1 if /\bps_Domain\b/;
push @data, $_;
} else {
last if $found;
@data = ();
}
}
print Dumper\@data;
__DATA__
Random network information.
cn_DomainIntity : ps_Domain
more text
10.162.55.123 RadioBearerSetup
some text
some more text
cs_Domain
random text
10.162.55.136 RadioBearerSetupComplete
network stats
10.162.55.122 RadioBearerSetup
text
ps_Domain
text
10.162.66.125 RadioBearerSetupComplete
...
...
<强>输出:强>
$VAR1 = [
'10.162.55.122 RadioBearerSetup',
'text',
'ps_Domain',
'text',
'10.162.66.125 RadioBearerSetupComplete'
];
答案 1 :(得分:0)
我可能会像这样处理你的问题:
E.g:
#!/usr/bin/perl
use strict;
use warnings;
local $/;
my @blobs = <> =~ m/([\d\.]+ RadioBearerSetup.*?RadioBearerSetupComplete)/mgs;
print grep { m/ps_Domain/ } @blobs;
结果:
10.162.55.122 RadioBearerSetup
text
ps_Domain
text
10.162.66.125 RadioBearerSetupComplete
答案 2 :(得分:0)
对于它的价值,这是我对它的看法
的代码强>
$VAR1 = {
'ps_Domain' => [
'10.162.55.122 RadioBearerSetup',
'text',
'ps_Domain',
'text',
'10.162.66.125 RadioBearerSetupComplete'
],
'cs_Domain' => [
'10.162.55.123 RadioBearerSetup',
'some text',
'some more text',
'cs_Domain',
'random text',
'10.162.55.136 RadioBearerSetupComplete'
]
};
输出显示按域
分隔的哈希docker run -it debian ping 172.17.0.15