-2

Let's say I have an array from file /some/file.json:

[
    "This.FilE",
    "That.file",
    "Another.FIle"
]

Then I have a string x with value ThiS.FIle.is.HeRe, now I wanted to know whether any of the string in the array matched the substring in x, case-insensitive (true/false result). Is there any clever way to do this with jq one-liner?

0

1 Answer 1

3

Use inside (the reverse operation to contains) on strings with unified case (e.g. ascii_downcase). any will evaluate to true if at least one item matches.

jq --arg x 'ThiS.FIle.is.HeRe' 'any(.[] | ascii_downcase; inside($x | ascii_downcase))'
true
Sign up to request clarification or add additional context in comments.

4 Comments

This also logs true if it's not in the array?
@0stone0 No, any defaults to false. See jq -n '[] | any'
@0stone0 That's correct. IIUC, true is the correct answer, as "This.FilE" is a (case-insensitive) part of "ThiS.FIle.is.HeRe".
Ahhh, didn't notices the substring, i'll get some coffee, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.