jq 结合 select 的正负过滤器

jq combine positive and negative filters with select

给定以下 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

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

.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"

Demo