2016-10-19 3 views
2

はここに答え値:JQ - どのようにプロパティの「ブラックリスト」に基づいてオブジェクトを選択するには、質問と同様に

... jq - How to select objects based on a 'whitelist' of property valuesを、私はプロパティ値のブラックリストに基づいてオブジェクトを選択したいのですが、次のホワイトリストとして正常に動作します: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」のほかに、著者からのコミットを否定したい、です。

[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" 

任意の提案:私は!=またはnotを使用している場合しかし、私は同じ間違った結果を得るように見えますか?

.[] | .author.login | select(. as $i | all($blacklist[]; $i != .)) 

あなたJQの場合:

.[] | .author.login | select(. as $i | $blacklist | index($i) | not) 

しかし、あなたJQはall/2を持っていると仮定し、それを使用するために言うことに何かがあります:

答えて

0

一つの解決策は、単にindexnotを用いることであろうそれがなければ、all/2は次のように定義されています。

def all(s; condition): reduce s as $i (true; . and ($i | condition)); 
+0

ありがとう、これは完全に動作します: – user284274

関連する問題