【发布时间】:2014-06-20 10:33:17
【问题描述】:
我已将文本文件“FilenameKeyword.txt”文件放在 E:/Test 文件夹中,在我的 perl 脚本中,我试图遍历该文件夹,并且我试图找到一个文件名具有字符串“关键字”,稍后我在脚本中打印了该文件的内容。 现在我希望对放置在压缩的 tar 文件中的文件做同样的事情。
我试图从中提取详细信息的假设文件: E:\test.tar.gz
想知道perl是否有可能在不解压/解压缩假设文件的情况下搜索和读取文件。如果不可能,我还将分配一些临时内存来解压文件,解压后应该删除来自特定文本文件的内容。
在互联网上搜索时,我可以使用 Archive::Extract 来提取和读取 gzip/tar 文件,这是 Perl 的新手——我真的很困惑我应该如何使用它。你能帮忙解决这个问题吗....
输入文件:FilenameKeyword.txt
脚本:
use warnings;
use strict;
my @dirs = ("E:\\Test\\");
my %seen;
while (my $pwd = shift @dirs) {
opendir(DIR,"$pwd") or die "Cannot open $pwd\n";
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files)
{
if (-d $file and ($file !~ /^\.\.?$/) and !$seen{$file})
{
$seen{$file} = 1;
push @dirs, "$pwd/$file";
}
next if ($file !~ /Keyword/i);
my $mtime = (stat("$pwd/$file"))[9];
print "$pwd$file";
print "\n";
open (MYFILE, "$pwd$file");
while (my $line = <MYFILE>){
#print $line;
my ($date) = split(/,/,$line,2);
if ($line =~ s!<messageText>(.+?)</messageText>!!is){
print "$1";
}
}
}
}
输出(在测试程序文件放在E:\Test下):
E:\Test\FilenameKeyword.txt
1311 messages Picked from the Queue.
寻求帮助以检索位于下的文件的内容 E:\test.tar.gz
期望的输出:
E:\test.tar.gz\FilenameKeyword.txt
1311 messages Picked from the Queue.
【问题讨论】:
-
“perl tar gzip”的快速谷歌搜索结果为perldoc.perl.org/Archive/Tar.html。看起来那个模块就是你想要的。
标签: perl perl-module