我正在编写一个Perl脚本来读取一个二进制文件,但存在问题。
我的代码如下,其中$file
是二进制格式的文件。我尝试在网上搜索并应用到我的代码中,尝试将其打印出来,但似乎它不能正常工作。
目前它只打印出'&&&&&&&&&&&"和""ppppppppppp",但我真正想要的是它能够打印出每个$line
,这样我可以稍后进行其他后处理。此外,我不太确定$data
是什么,因为我看到它是文章示例代码的一部分,假定是一个标量。我需要有人指出我的代码中错误的位置。以下是我所做的。
my $tmp = "$basedir/$key";
opendir (TEMP1, "$tmp");
my @dirs = readdir(TEMP1);
closedir(TEMP1);
foreach my $dirs (@dirs) {
next if ($dirs eq "." || $dirs eq "..");
print "---->$dirs\n";
my $d = "$basedir/$key/$dirs";
if (-d "$d") {
opendir (TEMP2, $d) || die $!;
my @files = readdir (TEMP2); # This should read binary files
closedir (TEMP2);
#my $buffer = "";
#opendir (FILE, $d) || die $!;
#binmode (FILE);
#my @files = readdir (FILE, $buffer, 169108570);
#closedir (FILE);
foreach my $file (@files) {
next if ($file eq "." || $file eq "..");
my $f = "$d/$file";
print "==>$file\n";
open FILE, $file || die $!;
binmode FILE;
foreach ($line = read (FILE, $data, 169108570)) {
print "&&&&&&&&&&&$line\n";
print "ppppppppppp$data\n";
}
close FILE;
}
}
}
我已经修改了代码,如下所示。现在我可以读取$data了。感谢J-16 SDiZ指出这一点。我试图将从二进制文件中获得的信息推送到一个名为"@array"的数组中,想要从该数组中grep数据以查找与"p04"匹配的字符串,但失败了。有人能指出错误所在吗?
my $tmp = "$basedir/$key";
opendir (TEMP1, "$tmp");
my @dirs = readdir (TEMP1);
closedir (TEMP1);
foreach my $dirs (@dirs) {
next if ($dirs eq "." || $dirs eq "..");
print "---->$dirs\n";
my $d = "$basedir/$key/$dirs";
if (-d "$d") {
opendir (TEMP2, $d) || die $!;
my @files = readdir (TEMP2); #This should read binary files
closedir (TEMP2);
foreach my $file (@files) {
next if ($file eq "." || $file eq "..");
my $f = "$d/$file";
print "==>$file\n";
open FILE, $file || die $!;
binmode FILE;
foreach ($line = read (FILE, $data, 169108570)) {
print "&&&&&&&&&&&$line\n";
print "ppppppppppp$data\n";
push @array, $data;
}
close FILE;
}
}
}
foreach $item (@array) {
#print "==>$item<==\n"; # It prints out content of binary file without the ==> and <== if I uncomment this.. weird!
if ($item =~ /p04(.*)/) {
print "=>$item<===============\n"; # It prints "=><===============" according to the number of binary file I have. This is wrong that I aspect it to print the content of each binary file instead :(
next if ($item !~ /^w+/);
open (LOG, ">log") or die $!;
#print LOG $item;
close LOG;
}
}
我已经按照以下方式更改了代码,但它仍然无法正确地通过检查“log”文件来grep“p04”。它确实grep整个文件,包括二进制文件,如“@ ^ @ ^ @ ^ @ ^ G ^ D ^ @ ^ @ ^ @ ^ ^ @ p04bbhi06 ^ @ ^ ^ @ ^ @ ^ @ ^ @ ^ @ hh ^ R ^ @ ^ @ ^ @ ^ ^ @ ^ @ ^ @ p04lohhj09 ^ @ ^ @ ^ @ ^ ^ @” 。 我期望的是它只grep带有p04的任何内容,例如grep p04bbhi06和p04lohhj09。以下是我的代码:
foreach my $file (@files) {
next if ($file eq "." || $file eq "..");
my $f = "$d/$file";
print "==>$file\n";
open FILE, $f || die $!;
binmode FILE;
my @lines = <FILE>;
close FILE;
foreach $cell (@lines) {
if ($cell =~ /b12/) {
push @array, $cell;
}
}
}
#my @matches = grep /p04/, @lines;
#foreach $item (@matches) {
foreach $item (@array) {
#print "-->$item<--";
open (LOG, ">log") or die $!;
print LOG $item;
close LOG;
}
autodie
。 - Brad Gilbertp04(.*)
匹配从"p04"到下一个换行符的任何内容。然后,你可以在$1
中获取这个"任何内容"。先不要纠缠于繁琐的目录处理,先集中精力在单个文件上想要得到什么。文件有多大?你只读取了前170MB。而且你一直在覆盖"日志"文件,所以它只包含最后一个文件的最后一项内容。 - mivk-B
的解释。(引用:“-B”文件是一个“二进制”文件(与-T相反)。) - clt60