In jq, I can select an item in a list fairly easily:
$ echo '["a","b","c","d","e"]' | jq '.[] | select(. == ("a","c"))'
Or if you prefer to get it as an array:
$ echo '["a","b","c","d","e"]' | jq 'map(select(. == ("a","c")))'
But how do I select all of the items that are not in the list? Certainly . != ("a","c")
does not work:
$ echo '["a","b","c","d","e"]' | jq 'map(select(. != ("a","c")))'
[
"a",
"b",
"b",
"c",
"d",
"d",
"e",
"e"
]
The above gives every item twice, except for "a"
and "c
"
Same for:
$ echo '["a","b","c","d","e"]' | jq '.[] | select(. != ("a","c"))'
"a"
"b"
"b"
"c"
"d"
"d"
"e"
"e"
How do I filter out the matching items?
. != "a" or . != "c"
. That of course would always be true so you're not seeing anything filtered. However you're getting duplicates now since you're using the comma operator. Remember, for every value produced from commas, the expression is reevaluated with the new values. Soselect(. != ("a","c"))
becomesselect(. != "a"), select(. != "c")
. Then it should be very clear what's happening. – Mudpack. != ("a","c")
is logic OR, where I was expecting logical AND (even though. == ("a","c")
is logical OR). – Penney("a","c")
is two values"a"
and"c"
. For any expression that uses it, copy the expression substituting the values"a"
and"c"
for the copies. – Mudpack