awk没有按预期打印变量

时间:2017-08-19 00:10:17

标签: variables awk

非常努力地让awk打印出以下变量。但不管我怎么试,

awk -F, -v x=$CLIENT_ID -v y=$BRANCH -v z=$UUID -v b=$HERMES_GROUP_CSV_ID 'BEGIN {
    OFS = ","; ORS = "\n"
    } {
        if (length($3) == 0) {
            printf "\nCLIENT $x at $y Linux System Time: $z Pacific Time: $b #####: Column 3, Row "; printf NR; printf " data missing in the Client $x group input csv. Please check\n"
        }
     }' ${INPUT_FILE}

它总是打印出来

CLIENT $x at $y Linux System Time: $z Pacific Time: $b #####: Column 3, Row 249 data missing in the Client $x group input csv. Please check

任何一位大师能开导吗?感谢。

1 个答案:

答案 0 :(得分:2)

您使用$x作为变量引用,但awk中的$是引用输入中的字段。变量在没有装饰的情况下使用,例如x。所以:

awk -F, -v x=$CLIENT_ID -v y=$BRANCH -v z=$UUID -v b=$HERMES_GROUP_CSV_ID 'BEGIN {
    OFS = ","; ORS = "\n"
    } {
        if (length($3) == 0) {
            print "\nCLIENT "x" at "y" Linux System Time: "z" Pacific Time: "b" #####: Column 3, Row "; printf NR; printf " data missing in the Client "x" group input csv. Please check\n"
        }
    }' ${INPUT_FILE}

这里引用了x,但事实并非如此:要点x不出现在引用的字符串中,以便可以将其扩展为变量。