How to ignore certain values and print the rest using jq cmd

Viewed 207

Here I'm trying to write jq command that should ignore the message check failed for material: and Error while scheduling, and print the rest.

The other values which I'm trying to print will get changing at every time. But these msg will remain constant (check failed for material:, Error while scheduling). We need to write a jq cmd that should ignore the mentioned message and print the rest/changing content. Please let me know if I'm not clear. Thanks in Advance !

[
   {
      "message":"check failed for material:",
      "detail":"Error performing",
      "level":"ERROR"
   },
   {
      "message":"check failed for material:",
      "detail":"Error performing command",
      "level":"ERROR"
   },
   {
      "message":"Error while scheduling",
      "detail":"Maximum limit reached",
      "level":"ERROR"
   },
   {
      "message":"Error while scheduling",
      "detail":"Maximum limit reached",
      "level":"ERROR"
   },
   {
      "message":"Duplicate error",
      "detail":"Found a mapping value where it is not allowed",
      "level":"ERROR"
   },
   {
      "message":"Invalid Merged Configuration",
      "detail":"Number of errors: 44",
      "level":"ERROR"
   }
]

On jqplay: https://jqplay.org/s/tCYiua9KzH

Desired output:

[
   {
      "message": "Duplicate error",
      "detail": "Found a mapping value where it is not allowed",
      "level": "ERROR"
   },
   {
      "message": "Invalid Merged Configuration",
      "detail": "Number of errors: 44",
      "level": "ERROR"
   }
]
1 Answers
map(select(
   .message |
   contains("check failed for material:") or contains("Error while scheduling") |
   not
))

Demo

Or for exact matches,

map(select(
   .message |
   . != "check failed for material:" and . != "Error while scheduling"
))

Demo

  • We don't want to eliminate the array, so we use map instead of .[]. (Alternatively, we could keep using .[] but wrap the whole in [ ... ].)

  • contains(A, B) check if both A and B are contained, rather than either.

  • And not negates the condition so we can eliminate the matching records instead of keeping them.

Related