jq negative select elements of array
Asked Answered
B

1

11

Take the data set as lines of arrays with values, e.g.:

[ "Dog", "cat", "Bird"]

I would like to get an array with all those values that do not match a regex but am unwilling to use negative capture groups (regex is more complicated than that) - so if my criteria would be that first letter must be capital, the output array should be ["cat"]

Barth answered 9/5, 2018 at 14:18 Comment(0)
T
19

all those values that do not match a regex

Sounds like test(_) | not is what you're looking for. Assuming you meant to write "first letter must NOT be a capital", the following filter could be used:

map(select(test("^[A-Z]")|not))
Trillium answered 9/5, 2018 at 14:33 Comment(5)
But then the output is just true/false rather than the value testedBarth
Surely you jest. Or maybe you misplaced a parenthesis?Trillium
he was refering to a filter, that is then popped into the correct selectLampley
@Lampley - In jq, all “functions” are filters.Trillium
in my case, jq '.[] | sort_by(.storedBytes) | .[] | select(has("retentionInDays")|not)' has worked for a payload like: ``` { "logGroups": [ { "logGroupName": "/aws-glue/crawlers", "retentionInDays": 1234 }, { "logGroupName": "/aws/ecs/application/metrics" } ] } ```Fizzy

© 2022 - 2024 — McMap. All rights reserved.