我的文件有框架1,框架2,框架3 ......框架100+等关键字,我想在每个框架之间提取数据并将其放在数组中。我试过了,但我没有按照我的需要得到。我一直在匹配线。试过两种方法。
my $key1 = "Frame 1" ;
my $key2 = "Frame 2";
while (<ReadHandle>){
if (/^$key1/../^$key2/){
@linearray=$_;
print "\n @linearray \n" ;
}
}
这将打印所有行。第二次尝试如下
my $key1 = "Frame" ;
while (<ReadHandle>){
if (/^$key1/){
@linearray=$_;
print "\n @linearray \n" ;
}
}
这将只打印匹配的行而不是数据。
我的文件如下所示
Frame 1: 140 bytes on wire (1120 bits), 140 bytes captured (1120 bits)
Encapsulation type: Ethernet (1)
Arrival Time: Jun 11, 2014 16:03:37.864278820 India Standard Time
Ethernet II, Src: Cisco_a9:94:0a (00:30:96:a9:94:0a), Dst: IPv6mcast_00:00:00:0d (33:33:00:00:00:0d)
Frame 2: 90 bytes on wire (720 bits), 90 bytes captured (720 bits)
Encapsulation type: Ethernet (1)
Internet Protocol Version 6, Src: ::200:1:1:2 (::200:1:1:2), Dst: ff02::1:ff01:1 (ff02::1:ff01:1)
0110 .... = Version: 6
等等......非常感谢任何帮助或建议。
答案 0 :(得分:3)
您可以使用数组数组
my @linearray;
while (<ReadHandle>){
push @linearray, [] if /^Frame/;
push @{ $linearray[-1] }, $_ if @linearray;
}
use Data::Dumper;
print Dumper \@linearray;
答案 1 :(得分:1)
如果您的文件不是很大,那么您可以将整个文件读入字符串并将其拆分为Frame
。这样,整个Frame将有一个数组元素。
use strict;
use warnings;
use Data::Dumper;
my $data = do{ undef $/; <DATA> };
my @linearray = split /(?m)(?=^Frame \d+:)/, $data;
print Dumper \@linearray;
__DATA__
Frame 1: 140 bytes on wire (1120 bits), 140 bytes captured (1120 bits)
Encapsulation type: Ethernet (1)
Arrival Time: Jun 11, 2014 16:03:37.864278820 India Standard Time
Ethernet II, Src: Cisco_a9:94:0a (00:30:96:a9:94:0a), Dst: IPv6mcast_00:00:00:0d (33:33:00:00:00:0d)
Frame 2: 90 bytes on wire (720 bits), 90 bytes captured (720 bits)
Encapsulation type: Ethernet (1)
Internet Protocol Version 6, Src: ::200:1:1:2 (::200:1:1:2), Dst: ff02::1:ff01:1 (ff02::1:ff01:1)
0110 .... = Version: 6
$VAR1 = [
'Frame 1: 140 bytes on wire (1120 bits), 140 bytes captured (1120 bits)
Encapsulation type: Ethernet (1)
Arrival Time: Jun 11, 2014 16:03:37.864278820 India Standard Time
Ethernet II, Src: Cisco_a9:94:0a (00:30:96:a9:94:0a), Dst: IPv6mcast_00:00:00:0d (33:33:00:00:00:0d)
',
'Frame 2: 90 bytes on wire (720 bits), 90 bytes captured (720 bits)
Encapsulation type: Ethernet (1)
Internet Protocol Version 6, Src: ::200:1:1:2 (::200:1:1:2), Dst: ff02::1:ff01:1 (ff02::1:ff01:1)
0110 .... = Version: 6
'
];