jq 将正向和负向过滤器与 select 结合起来

发布于 2025-01-12 08:49:07 字数 769 浏览 0 评论 0原文

给定以下 JSON 对象“test.json”:

{
    "results": [
        {"name": "package-dev_0.0.1-dev.er56ut.pkg"},
        {"name": "package-dev_0.0.23-master.qwk89mo.pkg"},
        {"name": "package_0.9.1-dev.til39aw.pkg"},
        {"name": "package_0.3.3-master.mbw80lk.pkg"}
    
    ]
}

我尝试使用 jq 来过滤键“name”包含“dev”但不包含“master”的实例,并输出“name”的值。

过滤键“name”包含“dev”的实例很好:

cat test.json | jq '.results[]|select(.name|contains("dev")) | .name'
"package-dev_0.0.1-dev.er56ut.pkg"
"package-dev_0.0.23-master.qwk89mo.pkg"
"package_0.9.1-dev.til39aw.pkg"

但是我没有成功删除键“name”包含“master”的实例:

cat test.json | jq '.results[]|select(.name|contains("dev")) | select(.name|contains("master"))|not'
false

Given the following JSON object "test.json":

{
    "results": [
        {"name": "package-dev_0.0.1-dev.er56ut.pkg"},
        {"name": "package-dev_0.0.23-master.qwk89mo.pkg"},
        {"name": "package_0.9.1-dev.til39aw.pkg"},
        {"name": "package_0.3.3-master.mbw80lk.pkg"}
    
    ]
}

I am trying to use jq to filter for instances where the key "name" contains "dev" but does not contain "master" and output the value of "name".

Filtering for instances where the key "name" contains "dev" is fine:

cat test.json | jq '.results[]|select(.name|contains("dev")) | .name'
"package-dev_0.0.1-dev.er56ut.pkg"
"package-dev_0.0.23-master.qwk89mo.pkg"
"package_0.9.1-dev.til39aw.pkg"

But I haven't been successful in removing instances where the key "name" contains "master":

cat test.json | jq '.results[]|select(.name|contains("dev")) | select(.name|contains("master"))|not'
false

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

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

发布评论

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

评论(1

带刺的爱情 2025-01-19 08:49:07

使用 not,这是一个内置 而不是运算符:

.results[] | select(.name | contains("dev") and (contains("master") | not)) | .name
"package-dev_0.0.1-dev.er56ut.pkg"
"package_0.9.1-dev.til39aw.pkg"

Demo

或者,您可以将测试收集在数组中并进行测试反对那个

.results[] | select([.name | contains("dev", "master")] == [true, false]) | .name
"package-dev_0.0.1-dev.er56ut.pkg"
"package_0.9.1-dev.til39aw.pkg"

演示

Use not, which is a builtin rather than an operator:

.results[] | select(.name | contains("dev") and (contains("master") | not)) | .name
"package-dev_0.0.1-dev.er56ut.pkg"
"package_0.9.1-dev.til39aw.pkg"

Demo

Alternatively, you can collect your tests in an array and test against that

.results[] | select([.name | contains("dev", "master")] == [true, false]) | .name
"package-dev_0.0.1-dev.er56ut.pkg"
"package_0.9.1-dev.til39aw.pkg"

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文