Using bash variables inside awk

Viewed 48

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
1 Answers

${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
Related