使用Perl从文件中查找关键字并输出到文本文件
我有一个包含路径列表的文本文件。使用Perl从文件中查找关键字并输出到文本文件,perl,unix,Perl,Unix,我有一个包含路径列表的文本文件。 e、 g path.txt /project/results/ver1/ /project/results/ver2/ /project/results/ver1000/ 在每个路径中,它都包含一个名为report.txt的文件。 我正在编写一个perl程序,逐行读取path.txt文件,然后进入path并grep report.txt文件。 我需要使用grep函数捕获文件中的关键字。 然后我将把结果提取到另一个文本文件中 我试着编写perl程序,
e、 g path.txt
/project/results/ver1/
/project/results/ver2/
/project/results/ver1000/
在每个路径中,它都包含一个名为report.txt的文件。
我正在编写一个perl程序,逐行读取path.txt文件,然后进入path并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);
}
my$output=(“output.txt”);
my$path=(“path.txt”);
打开(OUT,“>”,$output)或模具;
打开(路径,“my$output=“output.txt”;
my$path=“path.txt”;
打开(OUT,“>”,$output)或die$!;
打开(路径,为什么不使用一个简单明了的旧bash
:
while read path; do
grep 'Total Output Is' "$path"/report.txt
done < path.txt > output.txt
读取路径时;执行
grep“总输出为“'$path”/report.txt
完成output.txt
grep
在perl中与shell命令grep(1)
完全不同。它需要第二个参数中的列表。请参阅perldoc,它听起来像一个shell命令任务:cat path.txt;while read-r file;do grep'总输出为'$file/report.txt”;done>Output.txt
while read path; do
grep 'Total Output Is' "$path"/report.txt
done < path.txt > output.txt