2016-10-19 27 views
2

的问题类似的回答在这里对象:jq - How to select objects based on a 'whitelist' of property values,我想选择基于属性值的黑名单对象...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的作者提交;但是,如果我用!=not,我似乎得到同样的错误的结果:

[email protected]:~⟫ 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" 
[email protected]:~⟫ 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" 

有什么建议?

回答

0

一种解决方案是简单地使用indexnot

.[] | .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)); 
+0

谢谢,这完美的作品: – user284274