第一场比赛后的JQ停止

发布于 2025-01-25 23:35:53 字数 412 浏览 3 评论 0原文

我有一个大文件(约500兆字节),其中数据是JSON格式。

{
    "0001": [
        "aaaaa",
        "qqqqq"
    ],
    "0002": [
        "aaaaa"
    ],
    "0003": [
        "ccccc"
    ],
    "0004": [
        "bbbbb"
    ]
...
}

我需要从中提取:

aaaaa
qqqqq

目前,我进行以下jq -r'尝试。 “ 0001” | 。

如果已经找到输入,请告知我一种停止进一步侦察的方法。我知道第一个(输入|)有一个,但我不明白如何实现此命令。

I have a large file (about 500 megabytes) and the data in it is in JSON format.

{
    "0001": [
        "aaaaa",
        "qqqqq"
    ],
    "0002": [
        "aaaaa"
    ],
    "0003": [
        "ccccc"
    ],
    "0004": [
        "bbbbb"
    ]
...
}

I need to extract from it:

aaaaa
qqqqq

At the moment and I do the following jq -r 'try . "0001" | .[]' ./1.txt, it works, but the problem is that it takes a very long time because the search continues on through the whole file, instead of stopping immediately after the first match.

Please advise me a way to stop further scouting if an input has already been found. I know that there is a first(inputs | ), but I don't understand how to implements this command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

停滞 2025-02-01 23:35:53

如果事先知道相关密钥是JSON对象中的第一个密钥,则使用@pmf给出的使用-stream和first/1的解决方案适用;否则,可以对其进行适应如下:

jq --stream -n 'first(fromstream(1 | truncate_stream(inputs|select(.[0][0] =="0001"))))[]' input.json

If it is known beforehand that the relevant key is the first one in the JSON object, then the solution using --stream and first/1 as given by @pmf is applicable; otherwise, it could be adapted as follows:

jq --stream -n 'first(fromstream(1 | truncate_stream(inputs|select(.[0][0] =="0001"))))[]' input.json
夏夜暖风 2025-02-01 23:35:53

这适用于样本输入:

jq --null-input --raw-output --stream 'label $out | inputs | if .[0][0] == "0001" then (if length == 2 then .[1] else break $out end) else empty end' file

This works for the sample input:

jq --null-input --raw-output --stream 'label $out | inputs | if .[0][0] == "0001" then (if length == 2 then .[1] else break $out end) else empty end' file
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文