如何将bash变量传递给awk脚本

时间:2016-04-21 10:27:36

标签: awk gawk

# My vars 
  index=test
  type=book

我正在尝试将这些变量传递给awk

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

期望的输出

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

1 个答案:

答案 0 :(得分:1)

不要将变量嵌入字符串中:

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

您不能将index用作awk变量名称btw,因为这是awk函数的名称。

相关问题