2014-02-15 170 views
1

我有一个包含路径列表的文本文件。
e.g path.txt使用Perl从文件中查找关键字并输出到文本文件

/project/results/ver1/ 
/project/results/ver2/ 
/project/results/ver1000/ 

,并在每个路径它包含一个名为report.txt档。 我正在写一个perl程序来逐行读取path.txt文件并下降到路径中并grep文件report.txt。
我需要通过使用grep函数捕获文件中的关键字。
然后我会将我的结果提取到另一个文本文件。

我试过编写perl程序,它似乎不起作用。 请原谅我,因为我还是编程新手。

my $output = ("output.txt"); 
my $path = ("path.txt"); 
open (OUT,'>',$output) or die; 
open (PATH, '<',$path) or die; 
foreach (<PATH>){ 
chomp; 
$path1 = $_; 
chdir ("$path1"); 
my $ans = grep 'Total Output Is' , report.txt; 
print OUT "$ans\n"; 
chdir($pwd); 
} 
+0

'grep'在Perl是从外壳命令完全不同的grep'(1)'。它需要第二个参数中的LIST。请参阅perldoc,http://perldoc.perl.org/functions/grep.html – ernix

+0

,它听起来像一个shell命令任务:'cat path.txt |同时读取-r文件; grep'Total Output Is'“$ file/report.txt”; done> output.txt' – ernix

回答

1
my $output = "output.txt"; 
my $path = "path.txt"; 
open (OUT, '>', $output) or die $!; 
open (PATH, '<', $path) or die $!; 

while (my $path1 = <PATH>) { 
    chomp $path1; 

    open my $fh, "<", "$path1/report.txt" or die $!; 
    /Total Output Is/ and print OUT $_ while <$fh>; 
    close($fh); 
} 

close(OUT); 
close(PATH); 
0

为什么不使用简单而直接的普通老式bash

while read path; do 
    grep 'Total Output Is' "$path"/report.txt 
done <path.txt> output.txt 
相关问题