Chubaka
Chubaka

Reputation: 3155

awk not printing variables as expected

Trying really hard to get awk print out the following variables. But no matter how I tried,

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}

it always prints out

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

Could any guru enlighten? Thanks.

Upvotes: 0

Views: 471

Answers (1)

John Zwinck
John Zwinck

Reputation: 249592

You are using $x as a variable reference, but $ in awk is to reference fields in the input. Variables are used without decoration, like x. So:

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}

It looks like x is quoted here, but it is not: the point is to have x appear NOT in the quoted string, so that it can be expanded as a variable.

Upvotes: 2

Related Questions