How to regex match strings that do not contain a set of substrings

52 Views Asked by At

I am looking through 1000s of JSON files, and all of them have a property on them like this: "value": {some number},

I want to grep all of these JSON files, and match all cases where value is NOT equal to 1,2, or 3. So if "value": 10? Grep should return that. "value": 1? Grep should not return that.

The best regex I can get is .*\"value\": ((?!1)|(?!2)|(?!3)) But I must not know enough have negative lookaheads to use this correctly, am I close? Is there a better way to do NOT operations in regex?

Regular expression to match a line that doesn't contain a word is the closest answer I've found, but I cannot figure out how to apply it to this case where I have a set of substrings.

1

There are 1 best solutions below

1
Gilles Quénot On

With :

Input file:

cat file.json
[
    {
        "value": 1
    },
    {
        "value": 2
    },
    {
        "value": 3
    },
    {
        "value": 4
    },
    {
        "value": 5
    },
    {
        "value": 6
    },
    {
        "value": 7
    }
]

Output:

jq '.[] | select( .value != 1 and .value != 2 and .value != 3 )' file.json
{
  "value": 4
}
{
  "value": 5
}
{
  "value": 6
}
{
  "value": 7
}

Will works too with multiple JSON files.