如何将多个args传递给jq? [英] how to pass multiple args into jq?

查看:177
本文介绍了如何将多个args传递给jq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个新的json文件.我想定义多个变量,然后使用管道将它们设置为新json文件中的不同属性.

I am trying to write a new json file. I want to define multiple variables then set them with piping to different properties in the new json file.

jq --arg dnb "$DOMAIN_NAME_BUILT" --arg origin "$DOMAIN_ID_BUILT" \ 
   '.Origins.Items[0].DomainName = $dnb' | '.Origins.Items[0].Id = $origin' distconfig.json > "$tmp" && mv "$tmp" distconfig.json

这仅适用于一个变量:--arg NAME VALUE模式,但是当我添加第二个arg并使用管道jq ... 'x1 = y1 | x2 = y2, e.g.时,它将中断.

This works with just one variable: --arg NAME VALUE pattern, but when I add in a second arg and use piping jq ... 'x1 = y1 | x2 = y2, e.g. it breaks.

推荐答案

管道应在过滤器内.并且,请考虑使用赋值运算符来缩短您的代码:

pipe should be inside the filter. and, consider using assignment operator to shorten your code:

jq --arg dnb "$DOMAIN_NAME_BUILT" \
   --arg origin "$DOMAIN_ID_BUILT" \
   '.Origins.Items[0] |= ( .DomainName = $dnb | .Id = $origin )' \
distconfig.json > "$tmp" && mv "$tmp" distconfig.json

这篇关于如何将多个args传递给jq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆