在perl中执行grep、awk
我需要在perl脚本中执行以下任务在perl中执行grep、awk,perl,file,awk,grep,Perl,File,Awk,Grep,我需要在perl脚本中执行以下任务 'cat$temp_file | grep$specific_word | awk'{print\$2}' open(F,$temp_file); @list=<F>;close F; $specific_word ="String I want"; @f=grep /$specific_word/,@list; print $f[2]; 打开(F$temp_文件); @列表=;关闭F; $specific_word=“字符串我想要”; @f=g
'cat$temp_file | grep$specific_word | awk'{print\$2}'
open(F,$temp_file);
@list=<F>;close F;
$specific_word ="String I want";
@f=grep /$specific_word/,@list;
print $f[2];
打开(F$temp_文件);
@列表=;关闭F;
$specific_word=“字符串我想要”;
@f=grep/$specific_word/,@list;
打印$f[2];
我正在做上述工作。有人能帮我吗。有没有其他方法可以做到这一点
注意:我需要打印每一行的第二个字。根据以上代码,我只得到第二行。
perl -lane "print \$F[1] if /$specific_word/" $temp_file
如果你真的想长篇大论(确保你使用5.12.0
(或类似版本)来获得,说:
open my $f, '<', $temp_file;
while(<$f>) {
my @F = split;
say $F[1] if /$specific_word/;
}
打开我的$f,'Perl本机支持regex,我认为使用外部工具不会给你带来最好的结果。当你运行代码时会发生什么?请访问并阅读,了解如何有效地使用此网站。@JimGarrison编辑了这个问题,作为旁白,你开始使用的命令行很糟糕。你几乎从不需要d在同一管道中使用grep
和awk
,更不用说cat
。这应该是简单的:awk'/specific\u word/{print$2}'temp_file
@MichaelVehrs是的。但我只需要用perl的方式来做,我需要在我的系统中使用它script@VinodiniNatrajan您现在应该自己解决。@Vinodinatrajan脚本顶部的使用v5.10;
或使用功能“say”;
使您能够执行说“hello”
,而不是打印“hello\n”
我认为您可以通过打印+(拆分)[1]完全跳过@F
的使用如果m/$specific\u word/
,则可能值得使用quotemeta
或\Q\E
,这也取决于$specific\u word
的来源,因为从用户输入中添加一个损坏的正则表达式可能会导致各种wierdness。