我必须从日志文件中逐行提取两个符号(:
和;
)之间的数据。我的数据是:
INFO @3035155 mti_lane1_bw_mon: total bytes = 0, at time = 3035155; T=3035155
我正在使用此代码在冒号(:
)之后提取数据:
use strict;
use warnings;
my $filename = 'log1';
my @fields;
open(FILE, $filename) or die "Could not read from $filename, program halting.";
while(<FILE>)
{
chomp;
@fields = split(':', $_);
print "$fields[1]\n";
}
close FILE;
当前输出:
total bytes = 0, at time = 3035155; T=3035155
必填输出:
total bytes = 0, at time = 3035155
实际上,我必须逐行比较两个不同的日志文件,并打印log1中不在log2中的所有行。我使用了这种方法,但答案不正确。现在我更新的代码是:
#!/usr/bin/perl
use strict;
use warnings;
#use 5.012;
my $filename1 = 'log2';
my %a_links;
open(FILE, $filename1) or die "Could not read from $filename1, program halting.";
while(<FILE>)
{
chomp;
my @fields1 = split(/[:;]/, $_);
print "$fields1[1]\n";
$a_links{$fields1[1]} = undef;
}
close FILE;
my $filename2 = 'log1';
my @fields;
open(FILE, $filename2) or die "Could not read from $filename2, program halting.";
while(<FILE>)
{
chomp;
@fields = split(/[:;]/, $_);
next if exists $a_links{$fields[1]};
print "$fields[1] \n";
}
close FILE;
答案 0 :(得分:1)
您知道可以使用正则表达式进行分割吗?
尝试
@fields = split(/[:;]/, $_);