jq - 如何 select 基于 'blacklist' 的 属性 值的对象
jq - How to select objects based on a 'blacklist' of property values
类似于此处回答的问题:,我想 select 对象基于 属性 值的黑名单...
以下作为白名单工作正常:curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson whitelist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $whitelist[]) | {author: .author.login, message: .commit.message}'
{
"author": "dtolnay",
"message": "Remove David from maintainers"
}
{
"author": "stedolan",
"message": "Make jv_sort stable regardless of qsort details."
}
{
"author": "stedolan",
"message": "Add AppVeyor badge to README.md\n\nThanks @JanSchulz, @nicowilliams!"
}
问题是,我想否定这一点,只显示 'stedolan' 和 'dtolnay' 之外作者的提交;但是,如果我使用 !=
或 not
,我似乎会得到同样的错误结果:
nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $blacklist[] | not) | .author.login' | sort | uniq -c | sort -nr
14 "nicowilliams"
2 "stedolan"
1 "dtolnay"
nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login != $blacklist[]) | .author.login' | sort | uniq -c | sort -nr
14 "nicowilliams"
2 "stedolan"
1 "dtolnay"
有什么建议吗?
一个解决方案就是使用 index
和 not
:
.[] | .author.login | select( . as $i | $blacklist | index($i) | not)
但是,假设您的 jq 有 all/2
,使用它有话要说:
.[] | .author.login | select( . as $i | all($blacklist[]; $i != .))
如果你的jq没有,那么用这个方案还是有话要说的,all/2
定义如下:
def all(s; condition): reduce s as $i (true; . and ($i | condition));
类似于此处回答的问题:
以下作为白名单工作正常:curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson whitelist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $whitelist[]) | {author: .author.login, message: .commit.message}'
{
"author": "dtolnay",
"message": "Remove David from maintainers"
}
{
"author": "stedolan",
"message": "Make jv_sort stable regardless of qsort details."
}
{
"author": "stedolan",
"message": "Add AppVeyor badge to README.md\n\nThanks @JanSchulz, @nicowilliams!"
}
问题是,我想否定这一点,只显示 'stedolan' 和 'dtolnay' 之外作者的提交;但是,如果我使用 !=
或 not
,我似乎会得到同样的错误结果:
nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login == $blacklist[] | not) | .author.login' | sort | uniq -c | sort -nr
14 "nicowilliams"
2 "stedolan"
1 "dtolnay"
nhenry@BONHENRY:~⟫ curl -s 'https://api.github.com/repos/stedolan/jq/commits?per_page=10' | jq --argjson blacklist '["stedolan", "dtolnay"]' '.[] | select(.author.login != $blacklist[]) | .author.login' | sort | uniq -c | sort -nr
14 "nicowilliams"
2 "stedolan"
1 "dtolnay"
有什么建议吗?
一个解决方案就是使用 index
和 not
:
.[] | .author.login | select( . as $i | $blacklist | index($i) | not)
但是,假设您的 jq 有 all/2
,使用它有话要说:
.[] | .author.login | select( . as $i | all($blacklist[]; $i != .))
如果你的jq没有,那么用这个方案还是有话要说的,all/2
定义如下:
def all(s; condition): reduce s as $i (true; . and ($i | condition));