AWK不打印

时间:2014-11-07 17:37:33

标签: bash awk

我想知道为什么这个awk命令行没有打印FILENAME

WORD=abrego
NWORD=${#WORD}
awk -F", " -v var3=$NWORD -v var2="$WORD" -v var1="colombia" \
'gsub(/^c1 \[[^]]*\]/,",") \
$NF==var1 && (var3==length($(NF-2)) || var3==length($(NF-1))) && ($(NF-2)==var2 || $(NF-1)==var2) \
{print FILENAME}' file*

file1

c1 [gaston zamora-abrego, j.] univ nacl colombia sede medellin, dept ciencias forestales, medellin, colombia

file2

c1 [zamora-abrego, joan g.] univ nacl colombia, dept ciencias forest, grp ecol  conservac fauna silvestre, sede medellin, colombia

file3

c1 [gaston zamora-abrego, j.] univ nacl colombia, dept ciencias forestales, fac ciencias agr, medellin , antioquia, colombia

file4

c1 [perez, maritza] abrego, norte de santander, colombia

预期输出

file4

1 个答案:

答案 0 :(得分:1)

代码中的

gsub函数需要先执行才能使其他条件有效。

这应该有效:

awk -F", " -v var3=$NWORD -v var2="$WORD" -v var1="colombia" '{gsub(/^c1 \[[^]]*\]/,",")} 
  $NF==var1 && (var3==length($(NF-2)) || var3==length($(NF-1))) && 
    ($(NF-2)==var2 || $(NF-1)==var2) {print FILENAME}' file*