将两个jq过滤器合并为一个 [英] combine two jq filters into one

查看:61
本文介绍了将两个jq过滤器合并为一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jq过滤器如何组合过滤器输出?紧随jq之后,不会生成带有相应输入arg值("jack")的output.json.

How jq filter combines the filter outputs? Following jq not generates output.json with respective input arg value ('jack').

{
"key1": "",
"key2": ""
}

 jq --arg input "$username" \
          'if .key1 == "<value1>"
                 then . + {"key1" : ($input) }
          else . end' input.json |
          'if .key2 == "<value2>"
                 then . + {"key2" : ($input) }
          else . end'  >  output.json

output.json

{
"key1": "jack",
"key2": "jack"
}

推荐答案

您显然要编写的过滤器是:

The filter you are evidently trying to write is:

      if .key1 == "" then . + {"key1" : $input } else . end
      | if .key2 == "" then . + {"key2" : $input } else . end

这可以简化为:

      if .key1 == "" then .key1 = $input else . end
      | if .key2 == "" then .key2 = $input else . end

您可能还想考虑以下方法:

You might also like to consider the following approach:

      def update(f): f |= (if . == "" then $input else . end);
      update(.key1) | update(.key2)

这篇关于将两个jq过滤器合并为一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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