Tuesday, October 25, 2022

[SOLVED] extract lines from a file that do not fulfill certain criteria

Issue

in the following file, i need to get all lines that do not have in them the following pattern:

\"\d\d\d\":[

i do not want the line "memo": { or "notice": "delivered on 17", i just want the lines that have \"\d\d\d\":[, but instead they have \"\d\d\":[, or \"\s\d\d\":[ etc. How could this be done with sed or awk, or even better with jq?

i just need the lines that do not have in them the \"\d\d\d\":[ pattern, for example \"42 \":[ should be reported. Each message line, must have \"\d\d\d\":[ and not any other variation. For example \"42a\":[ is also wrong, and should be reported

{
  "memo": {
    "notice": "delivered on 17"
  },
  "message": "{\"id\":\"1\",\"401\":[[\" 0\",[\"a\",\"UK\"],[\"b\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
  "memo": {
    "notice": "delivered on 190"
  },
  "message": "{\"id\":\"2\",\"424\":[[\" 0\",[\"v\",\"UK\"],[\"9\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
  "memo": {
    "notice": "delivered on 734"
  },
  "message": "{\"id\":\"3\",\"432\":[[\" 0\",[\"a\",\"UK\"],[\"9\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"v\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}
{
  "memo": {
    "notice": "delivered on 1092"
  },
  "message": "{\"id\":\"888\",\"5 2\":[[\" 0\",[\"v\",\"UK\"],[\"b\",\"Euy/ O\"],[\"c\",\"20160811\"],[\"g\",\"R2\"]],[\" 1\",[\"a\",\"UK\"],[\"b\",\"LO\"],[\"c\",\"20160811\"]]]}"
}

as an output, I need the id value of the lines that have these problems.

expected output of the above: id: 888 since in this line there is the erroneous \"5 2\":[


Solution

Decode all messages, select those which do not contain any key matching \d{3}, and extract their ids.

jq '.message | fromjson | select(any(keys_unsorted[]; test("[0-9]{3}")) | not) .id' file


Answered By - oguz ismail
Answer Checked By - Willingham (WPSolving Volunteer)