Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 文件每行上的Shell命令_Perl_Shell_Scripting - Fatal编程技术网

Perl 文件每行上的Shell命令

Perl 文件每行上的Shell命令,perl,shell,scripting,Perl,Shell,Scripting,我正在尝试编写一个Perl脚本,它将执行以下操作: 1.打开一个文件,其中每行包含多个文件的位置(例如文件a、文件B) 2.对这些文件中的每个文件执行一个egrep,例如egrep“模块|输入|输出” 3.使用相同的文件名A、B等将egrep的输出存储在不同的目录中) egrep上有更多的约束,但我可以稍后添加它们 我的问题是如何执行步骤2和步骤3?在中,我在Perl脚本打开的文件的每一行上执行一个shell命令。文件位置将是,例如,design/rtl/name.v,我希望egrep的结果存储

我正在尝试编写一个Perl脚本,它将执行以下操作:
1.打开一个文件,其中每行包含多个文件的位置(例如文件a、文件B)
2.对这些文件中的每个文件执行一个egrep,例如egrep“模块|输入|输出”
3.使用相同的文件名A、B等将egrep的输出存储在不同的目录中)

egrep上有更多的约束,但我可以稍后添加它们


我的问题是如何执行步骤2和步骤3?在中,我在Perl脚本打开的文件的每一行上执行一个shell命令。文件位置将是,例如,
design/rtl/name.v
,我希望egrep的结果存储在
design/fake/name.v
文件名循环中,如下所示:

while read file
do
   egrep "module|imput|ouput" "$file" > "/another/directory/${file##*/}" 
done < fileList.txt
读取文件时
做
egrep“模块|输入|输出“$file”>”/other/directory/${file##*/}”
完成
像这样循环文件名:

while read file
do
   egrep "module|imput|ouput" "$file" > "/another/directory/${file##*/}" 
done < fileList.txt
读取文件时
做
egrep“模块|输入|输出“$file”>”/other/directory/${file##*/}”
完成
在shell(bash)中创建一个目录结果,并在文件“list”中的一行中列出您的文件,然后在bash提示符下直接键入

for i in `cat list`;do egrep "string" $i > result/grep${i}; done
这里与perl中的类似。错误行为更好,可能更快一点:真正的优势是可以更容易地添加额外的行为。将其另存为文件并作为“perl脚本列表foo”运行,其中script是脚本的文件名,list是文件列表的文件名,foo是正在搜索的模式

#!/usr/bin/perl
#

my ( $file, $pattern ) = @ARGV;

open( my $fh, $file ) || "die $! $file\n";

while (<$fh>) {
    chomp;    #trim newline from filename
    my $togrep = $_;
    open( my $grepfh, $togrep ) || warn "$togrep $!";
    if ($grepfh) {
        open( my $output, ">result/$togrep" ) || die "$togrep $!";
        while ( grep( $pattern, <$grepfh> ) ) {
            print $output $_;
        }
    }
}
#/usr/bin/perl
#
我的($file,$pattern)=@ARGV;
打开(我的$fh,$file)| |“die$!$file\n”;
而(){
chomp;#从文件名修剪换行符
my$togrep=$\;
打开(my$grepfh,$togrep)| |警告“$togrep$!”;
如果($grepfh){
打开(我的$output,“>result/$togrep”)| | die“$togrep$!”;
而(grep($pattern,)){
打印$output$;
}
}
}
在shell(bash)中创建一个目录结果,并在文件“list”中的一行中列出您的文件,然后在bash提示符下直接键入

for i in `cat list`;do egrep "string" $i > result/grep${i}; done
这里与perl中的类似。错误行为更好,可能更快一点:真正的优势是可以更容易地添加额外的行为。将其另存为文件并作为“perl脚本列表foo”运行,其中script是脚本的文件名,list是文件列表的文件名,foo是正在搜索的模式

#!/usr/bin/perl
#

my ( $file, $pattern ) = @ARGV;

open( my $fh, $file ) || "die $! $file\n";

while (<$fh>) {
    chomp;    #trim newline from filename
    my $togrep = $_;
    open( my $grepfh, $togrep ) || warn "$togrep $!";
    if ($grepfh) {
        open( my $output, ">result/$togrep" ) || die "$togrep $!";
        while ( grep( $pattern, <$grepfh> ) ) {
            print $output $_;
        }
    }
}
#/usr/bin/perl
#
我的($file,$pattern)=@ARGV;
打开(我的$fh,$file)| |“die$!$file\n”;
而(){
chomp;#从文件名修剪换行符
my$togrep=$\;
打开(my$grepfh,$togrep)| |警告“$togrep$!”;
如果($grepfh){
打开(我的$output,“>result/$togrep”)| | die“$togrep$!”;
而(grep($pattern,)){
打印$output$;
}
}
}

为什么要使用perl来做真正应该是shell脚本的事情?正如前面所述,这更多的是一个shell脚本问题,但是,使用Perl自己的文件读取和搜索功能,而不是调用
egrep
,可能会更好。为什么要使用Perl来做真正应该是shell脚本的事情呢?正如前面所描述的,这更像是一个shell脚本问题,但是使用Perl自己的文件读取和搜索功能,而不是调用
egrep
,可能会更好。