Shell 脚本 - 如果在 Bash 中匹配,则迭代、比较并提取 JSON 的值

Shell script - Iterate, compare and extract the value of JSON if matched in Bash

我有下面的静态 JSON 文件,其中提供了错误类型和操作的列表 (errors.json),并且 我有一个只有 1 种错误类型的日志文件 (logfile.log).

我想遍历下面的 JSON 数组并将每个错误与日志文件中的错误进行比较。如果以下任何错误与日志文件匹配,那么我需要打印操作

[  {
      "error": "timeout error",
      "action": "do this"
    },
    {
      "error": "file not found",
      "action": "do that"
    },
    {
      "error": "connectivity error",
      "action": "foo"
    },
    {
      "error": "EOF exception",
      "action": "blaaa"
    }
]

这可能类似于:

while IFS= read -r -d '' error && IFS= read -r -d '' action; do
  if grep -e "$error" <yourfile; then
    echo "Need to do action: $action"
  fi
done < <(jq -j '.[] | (.error, "\u0000", .action, "\u0000")')