我有一个 aws 查询,我想在
jq
中进行过滤。
我想过滤所有 imageTags
不 以“最新”结尾的
到目前为止,我这样做了,但它过滤了包含“最新”的内容,而我想过滤不包含“最新”的内容(或不以“最新”结尾)
aws ecr describe-images --repository-name <repo> --output json | jq '.[]' | jq '.[]' | jq "select ((.imagePushedAt < 14893094695) and (.imageTags[] | contains(\"latest\")))"
谢谢
您可以使用
not
来反转逻辑
(.imageTags[] | contains(\"latest\") | not)
此外,我想您可以将管道简化为单个
jq
调用。
| not
一个有用的示例,特别是对于 mac
brew
用户:
通过查询 JSON 并解析输出
brew info --json=v1 --installed | jq -r 'map(
select(.installed[].poured_from_bottle)|.name) | unique | .[]' | tr '\n' ' '
通过查询 JSON 并解析输出并使用
| not
brew info --json=v1 --installed | jq -r 'map(
select(.installed[].poured_from_bottle | not) | .name) | unique | .[]'
在这种情况下
contains()
无法正常工作,最好使用not
功能的index()
select(.imageTags | index("latest") | not)
这个
.[] | .[]
可以缩写为 .[][]
例如,
$ jq --null-input '[[1,2],[3,4]] | .[] | .[]'
1
2
3
4
$ jq --null-input '[[1,2],[3,4]] | .[][]'
1
2
3
4
要检查一个字符串是否不包含另一个字符串,您可以组合
contains
和 not
,例如,
$ jq --null-input '"foobar" | contains("foo") | not'
false
$ jq --null-input '"barbaz" | contains("foo") | not'
true
您可以使用
any
或 all
对字符串数组执行类似的操作,例如,
$ jq --null-input '["foobar","barbaz"] | any(.[]; contains("foo"))'
true
$ jq --null-input '["foobar","barbaz"] | any(.[]; contains("qux"))'
false
$ jq --null-input '["foobar","barbaz"] | all(.[]; contains("ba"))'
true
$ jq --null-input '["foobar","barbaz"] | all(.[]; contains("qux"))'
false
假设你有 file.json:
[ [["foo", "foo"],["foo", "bat"]]
, [["foo", "bar"],["foo", "bat"]]
, [["foo", "baz"],["foo", "bat"]]
]
并且您只想保留没有任何字符串的嵌套数组
"ba"
:
$ jq --compact-output '.[][] | select(all(.[]; contains("bat") | not))' file.json
["foo","foo"]
["foo","bar"]
["foo","baz"]