0

I have the below static JSON file where list of error types and actions are provided (errors.json), and i have a log file which will have only 1 error type(logfile.log).

I wanted to iterate through the below JSON array and compare each error with the errors in the log file. If any of the below errors match with log file, then i need the action to be printed

[  {
      "error": "timeout error",
      "action": "do this"
    },
    {
      "error": "file not found",
      "action": "do that"
    },
    {
      "error": "connectivity error",
      "action": "foo"
    },
    {
      "error": "EOF exception",
      "action": "blaaa"
    }
]
upstock
  • 1
  • 1
  • bash, or sh? They're two different languages; tag for one or the other, but not both. ("sh" is POSIX sh, a specification all standards-compliant shells are required to comply with; bash is a specific, _extended_ shell with a bunch of features POSIX sh doesn't require, and also a few minor deviations from that spec). – Charles Duffy Oct 12 '20 at 22:34
  • ...beyond that, this question is rather unacceptably broad. It's easy to iterate over the pairs (using `jq`, as described in existing Q&A entries on the topic), but we don't know what your "compare each error with the errors in the log file" code needs to look like, because we don't know the format of your log files. – Charles Duffy Oct 12 '20 at 22:35

1 Answers1

1

This might look something like:

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")')
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441