jq 将正向和负向过滤器与 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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
not
,这是一个内置 而不是运算符:Demo
或者,您可以将测试收集在数组中并进行测试反对那个
演示
Use
not
, which is a builtin rather than an operator:Demo
Alternatively, you can collect your tests in an array and test against that
Demo