0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@EdMorton: Good point, but in this case it should be print instead of printf, since OP is not using format strings actually. Updated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.