在Perl脚本中使用AWK?

时间:2012-12-05 18:34:09

标签: perl awk

这就是我要做的事情:

$variable = `grep -i "Text: Added to directory" '$FOO/result.txt' | awk '{print $6}' | tr -d "'"`
print $variable;

输出:

Text: Added to directory /path/to/directory/
Use of uninitialized value $6 in concatenation (.) or string

如何才能获取“/ path / to / directory”而不是“Text:添加到目录/ path / to / directory /”?

2 个答案:

答案 0 :(得分:4)

当然Perl可以做grep,awk和tr所能做的事情。

open my $fh, "<", "$FOO/result.txt" or die "can't open file: $!\n";
while (<$fh>) {
    next unless /pattern/i;
    (my $six = (split)[5]) =~ tr/'//d;
    print $six, "\n";
}
close $fh;

答案 1 :(得分:1)

您可能需要调整引用转义但这应该是:

awk IGNORECASE=1 '/yourpattern/{ gsub(/\'/, \'\'); print $6 }' $FOO/result.txt

AWK非常多才多艺。