Chubaka
Chubaka

Reputation: 3155

Using bash variables inside awk

Trying to pass the external "CLIENT_ID" variable into this line of awk, but it doesn't work as expected. This is what I've tried:

CLIENT_ID=1
awk -v CLIENT_ID="${CLIENT_ID}" 'NF{print "xxx_${CLIENT_ID}_" $0 ".sh"}' A.csv > B.csv

in A.csv

201712
201711
201710

Desired output in B.csv:

xxx_1_201712.sh
xxx_1_201711.sh
xxx_1_201710.sh

What I'm currently getting:

xxx_${CLIENT_ID}_201712.sh
xxx_${CLIENT_ID}_201711.sh
xxx_${CLIENT_ID}_201710.sh

Upvotes: 6

Views: 74

Answers (1)

that other guy
that other guy

Reputation: 123680

${CLIENT_ID} is bash syntax. You should instead use the awk syntax you're already using for $0:

awk -v CLIENT_ID="${CLIENT_ID}" 'NF{print "xxx_" CLIENT_ID "_" $0 ".sh"}' A.csv > B.csv

Upvotes: 8

Related Questions