AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 744139
Accepted
Kusalananda
Kusalananda
Asked: 2023-04-26 23:26:47 +0800 CST2023-04-26 23:26:47 +0800 CST 2023-04-26 23:26:47 +0800 CST

如何参数化 `jq` 表达式以返回选择或其补码?

  • 772

让我们假设我有两个非常复杂的jq表达式,但它们的不同之处仅在于一个返回另一个的补码,即它们之间的唯一区别是一个做select(expression)而另一个做select(expression|not)。

简化示例:

$ jq -n '$ARGS.positional[] | select( . > 2 )' --jsonargs 1 2 3 4 5
3
4
5
$ jq -n '$ARGS.positional[] | select( . > 2 | not )' --jsonargs 1 2 3 4 5
1
2

与其jq在我的代码中重复这两个不同的表达式(实际上,每个表达式只有几行),不如将一个值传递到单个表达式中以在两种行为之间切换会很整洁。

我怎样才能做到这一点?


实际jq代码(根据消息负载中编码的文件路径过滤 RabbitMQ 消息):

map(
        # Add an array of pathnames that would match this message.  This
        # includes the pathnames of each parent directory, leading up to
        # and including the pathname of the file itself.
        .tmp_paths = [
                # The full pathname is part of a base64-encodod JSON blob.
                foreach (
                        .payload |
                        @base64d |
                        fromjson.filepath |
                        split("/")[]
                ) as $elem (
                        null;
                        . += $elem + "/";
                        .
                )

        ] |
        # The last element is the full file path and should not have a
        # trailing slash.
        .tmp_paths[-1] |= rtrimstr("/")
) |
[
        # Match the pathnames given as positional command line arguments
        # against the computed pathnames in the "tmp_paths" array in
        # each message.  Extract the messages with a match.
        JOIN(
                INDEX($ARGS.positional[]; .);
                .[];
                .tmp_paths[];
                if (.[1:] | any) then
                        .[0]
                else
                        empty
                end
        )
] |
# Deduplicate the extracted messages on the full pathname of the file.
# Then remove the "tmp_paths" array from each message and base64 encode
# them.
unique_by(.tmp_paths[-1])[] |
del(.tmp_paths) |
@base64

我假设我需要if以某种方式修改该语句以使其提取或丢弃其文件路径与作为位置参数给出的路径名匹配的消息。

jq
  • 2 2 个回答
  • 58 Views

2 个回答

  • Voted
  1. Best Answer
    Kusalananda
    2023-04-26T23:26:47+08:002023-04-26T23:26:47+08:00

    将布尔值传递到jq表达式中,并使用if- 语句在返回选定集或其补集之间切换:

    $ jq -n --argjson yes true '$ARGS.positional[] | select( . > 2 | if $yes then . else not end )' --jsonargs 1 2 3 4 5
    3
    4
    5
    
    $ jq -n --argjson yes false '$ARGS.positional[] | select( . > 2 | if $yes then . else not end )' --jsonargs 1 2 3 4 5
    1
    2
    

    在更复杂的jq表达式中,修改if语句:

    # Match the pathnames given as positional command line arguments
    # against the computed pathnames in the "tmp_paths" array in
    # each message.  Depending on the $yes boolean variable, extract
    # or discard matching messages.
    JOIN(
            INDEX($ARGS.positional[]; .);
            .[];
            .tmp_paths[];
            if (.[1:] | any | if $yes then . else not end) then
                    .[0]
            else
                    empty
            end
    )
    

    请注意,withif $yes then . else not end允许变量充当$yes我们想要一个集合还是它的补集的“切换”。在简化的select()和更复杂的中JOIN(),此if语句作用于布尔测试结果,该结果确定元素是否应成为结果集的一部分。

    • 2
  2. LL3
    2023-04-29T22:59:16+08:002023-04-29T22:59:16+08:00

    @Kusalananda 描述的解决方案可以说是适用于所有常见情况的最佳方式,尤其是对于偶尔出现的情况,因为它简单、可读、紧凑,而且速度相当快。

    如果您经常使用这种切换行为以保证稳定的设置,或者如果您愿意多走一英里以获得一些速度,您可能会考虑采用不同的方法。

    if ... then ... else ... end事实上,a 的这种简单方法有一个缺点,即为流中的每个对象添加额外的比较。这种比较恰好有点浪费,因为它的结果总是预先知道的,是来自命令行的静态输入,在执行过程中永远不会改变。

    删除该比较的一种可能方法是使用模块中定义的函数,然后您可以在命令行中选择该函数。

    考虑:

    # let's set the thing up
    $ mkdir dot && echo 'def dot_or_not: .;' > dot/.jq
    $ mkdir not && echo 'def dot_or_not: not;' > not/.jq
    # now let's use it
    $ seq 5 | jq 'include "./"; select ( . > 2 | dot_or_not )' -Ldot
    3
    4
    5
    $ seq 5 | jq 'include "./"; select ( . > 2 | dot_or_not )' -Lnot
    1
    2
    

    在单处理器 VM 上的一些简单基准测试中,这种方法的结果平均比 bare 快 5 倍if ... then ... else ... end,尽管它可能不会影响您所展示的更大计算的“经济性”。

    我自己可能不会为这样一个简单的操作走这么远……因为在其他可能的(更有价值的)模块之上处理每个额外的“开关”时它会变得越来越麻烦和笨拙。事实上,我宁愿使用模块来进行真正不同的计算变体……但仍然如此。


    只是为了完整起见,在频谱的另一端,另一种方法可能是这样的:

    $ seq 5 | jq 'select( . > 2 | [not,.][$yes] )' --argjson yes 1
    3
    4
    5
    $ seq 5 | jq 'select( . > 2 | [not,.][$yes] )' --argjson yes 0
    1
    2
    

    或其表亲变体:

    $ seq 5 | jq 'select( . > 2 | {(tostring):1}[$yes] )' --arg yes true 
    3
    4
    5
    $ seq 5 | jq 'select( . > 2 | {(tostring):1}[$yes] )' --arg yes false 
    1
    2
    

    尽管这些方法看起来很紧凑,但它们也恰好比裸 慢得多(在我的简单基准测试中慢 2-4 倍)if ... then ... else ... end,因为它们中的每一个都添加了 2 个易失对象的构造和一个查找。

    • 0

相关问题

  • 使用 jq 获取结构化输出

  • 如何将嵌入(引用)的 json 字符串转换为 json

  • JSON数组使用jq来bash变量

  • jq 打印子对象中所有的键和值

  • jq 使用多个 --arg 添加或更新一个值

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve