如何忽略某些值并使用jq cmd打印其余值

  • 本文关键字:cmd jq 打印 余值 何忽略 jq
  • 更新时间 :
  • 英文 :


这里我正在尝试编写jq命令,该命令应该忽略消息check failed for material:Error while scheduling,并打印其余部分。

我尝试打印的其他值每次都会发生变化。但是这些消息将保持恒定(check failed for material:Error while scheduling(。我们需要编写一个jq cmd,它应该忽略提到的消息,并打印其余/更改的内容。如果我不清楚,请告诉我。提前感谢!

[
{
"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"
}
]

在jqplay上:https://jqplay.org/s/tCYiua9KzH

期望输出:

[
{
"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"
}
]
map(select(
.message |
contains("check failed for material:") or contains("Error while scheduling") |
not
))

演示

或者为了精确匹配,

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

演示

  • 我们不想消除数组,所以我们使用map而不是.[]。(或者,我们可以继续使用.[],但将整个包在[ ... ]中。(

  • contains(A, B)检查是否同时包含A和B,而不是其中之一。

  • not否定了这个条件,这样我们就可以消除匹配的记录,而不是保留它们。

最新更新