使用参数过滤数据

filtering data using parameters

我有这个命令正在运行..

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="39583746-39747586") | .lifeCycleState '

我想将实体键作为变量传递,尝试了以下方法,但 none 似乎有效-

enkey="39583746-39747586"

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="""${enkey}""") | .lifeCycleState '

cat  ~/Desktop/results.json |  jq '.[] | .environmentStatuses[].deploymentResult | select(.key.entityKey.key=="${enkey}") | .lifeCycleState '

尝试在过滤器中使用额外参数时,请使用 --arg 选项将它们传入。不要依赖 shell 将其插入过滤器字符串,将其分开.

jq --arg key "$enkey" '.[] |
  .environmentStatuses[].deploymentResult |
  select(.key.entityKey.key == $key) |
  .lifeCycleState' ~/Desktop/results.json

这对我有用:

  jq '.[] | .environmentStatuses[].deploymentResult |
  select(.key.entityKey.key == "'$key'") |
  .lifeCycleState' ~/Desktop/results.json 

--arg 没有按预期工作...