0
# My vars index=test type=book 

I'm trying to pass these variables to awk

 awk -v index="$index" -v type="$type" 'BEGIN{print "{\"create\": {\"_index\":\"index\", \"_type\":\"type\"}}"}; {print}; END {printf "\n"}' 

Desired output

{"create": {"_index":"test", "_type":"book"}} 

1 Answer 1

1

Don't embed the variables in the string:

awk -v idx="$index" -v type="$type" 'BEGIN{print "{\"create\": {\"_index\":\"" idx "\", \"_type\":\"" type "\"}}"} {print} END {print ""}' 

You can't use index as an awk variable name, btw, as that's the name of an awk function.

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

1 Comment

Thanks Ed, you are always helpful

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.