jq - 如何根据属性值的“黑名单”选择对象

2023-12-13

类似于这里回答的问题:jq - 如何根据属性值的“白名单”选择对象,我想根据属性值黑名单选择对象......

以下内容可以很好地作为白名单: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”之外的作者的提交;但是,如果我使用!= or 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 with 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));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jq - 如何根据属性值的“黑名单”选择对象 的相关文章

随机推荐