我必须提取一个车牌数字,它与 JSON 字符串相关的置信度如下所示:
{
"response": {
"container": {
"id": "0df307bc-06b2-45cf-b7ff-ce07fd04e04d",
"timestamp": "2018-Jul-10 17:34:27.448632"
},
"id": "00000002-0000-0000-0000-000000000015"
},
"frames": {
"frame": {
"id": "5583",
"timestamp": "2016-Nov-30 13:05:27",
"lps": {
"lp": {
"licenseplate": "15451BBL",
"text": "15451BBL",
"wtext": "15451BBL",
"confidence": "20",
"bkcolor": "16777215",
"color": "16777215",
"type": "0",
"ntip": "11",
"cct_country_short": "",
"cct_state_short": "",
"tips": {
"tip": {
"poly": {
"p": { "x": "1094", "y": "643" },
"p": { "x": "1099", "y": "643" },
"p": { "x": "1099", "y": "667" },
"p": { "x": "1094", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "49",
"code_ascii": "1",
"confidence": "97"
},
"tip": {
"poly": {
"p": { "x": "1103", "y": "642" },
"p": { "x": "1113", "y": "642" },
"p": { "x": "1112", "y": "667" },
"p": { "x": "1102", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "53",
"code_ascii": "5",
"confidence": "89"
},
"tip": {
"poly": {
"p": { "x": "1112", "y": "640" },
"p": { "x": "1122", "y": "640" },
"p": { "x": "1122", "y": "666" },
"p": { "x": "1112", "y": "666" }
},
"bkcolor": "16777215",
"color": "0",
"code": "52",
"code_ascii": "4",
"confidence": "97"
},
"tip": {
"poly": {
"p": { "x": "1123", "y": "640" },
"p": { "x": "1132", "y": "640" },
"p": { "x": "1131", "y": "665" },
"p": { "x": "1123", "y": "665" }
},
"bkcolor": "16777215",
"color": "0",
"code": "53",
"code_ascii": "5",
"confidence": "97"
},
"tip": {
"poly": {
"p": { "x": "1134", "y": "640" },
"p": { "x": "1139", "y": "640" },
"p": { "x": "1139", "y": "664" },
"p": { "x": "1133", "y": "664" }
},
"bkcolor": "16777215",
"color": "0",
"code": "49",
"code_ascii": "1",
"confidence": "77"
},
"tip": {
"poly": {
"p": { "x": "1154", "y": "639" },
"p": { "x": "1163", "y": "639" },
"p": { "x": "1163", "y": "663" },
"p": { "x": "1153", "y": "663" }
},
"bkcolor": "16777215",
"color": "0",
"code": "66",
"code_ascii": "B",
"confidence": "97"
},
"tip": {
"poly": {
"p": { "x": "1164", "y": "638" },
"p": { "x": "1173", "y": "638" },
"p": { "x": "1173", "y": "663" },
"p": { "x": "1163", "y": "663" }
},
"bkcolor": "16777215",
"color": "0",
"code": "66",
"code_ascii": "B",
"confidence": "94"
},
"tip": {
"poly": {
"p": { "x": "1191", "y": "637" },
"p": { "x": "1206", "y": "636" },
"p": { "x": "1205", "y": "660" },
"p": { "x": "1190", "y": "661" }
},
"bkcolor": "16777215",
"color": "0",
"code": "76",
"code_ascii": "L",
"confidence": "34"
},
"tip": {
"poly": {
"p": { "x": "1103", "y": "655" },
"p": { "x": "1111", "y": "655" },
"p": { "x": "1111", "y": "667" },
"p": { "x": "1103", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "74",
"code_ascii": "J",
"confidence": "57"
},
"tip": {
"poly": {
"p": { "x": "1103", "y": "655" },
"p": { "x": "1111", "y": "655" },
"p": { "x": "1111", "y": "667" },
"p": { "x": "1103", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "74",
"code_ascii": "J",
"confidence": "57"
},
"tip": {
"poly": {
"p": { "x": "1176", "y": "638" },
"p": { "x": "1185", "y": "637" },
"p": { "x": "1184", "y": "661" },
"p": { "x": "1175", "y": "662" }
},
"bkcolor": "16777215",
"color": "0",
"code": "52",
"code_ascii": "4",
"confidence": "7"
}
},
"ncharacter": "8",
"characters": {
"characater": {
"poly": {
"p": { "x": "1094", "y": "643" },
"p": { "x": "1099", "y": "643" },
"p": { "x": "1099", "y": "667" },
"p": { "x": "1094", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "49",
"code_ascii": "1",
"confidence": "97"
},
"characater": {
"poly": {
"p": { "x": "1103", "y": "642" },
"p": { "x": "1113", "y": "642" },
"p": { "x": "1112", "y": "667" },
"p": { "x": "1102", "y": "667" }
},
"bkcolor": "16777215",
"color": "0",
"code": "53",
"code_ascii": "5",
"confidence": "89"
},
"characater": {
"poly": {
"p": { "x": "1112", "y": "640" },
"p": { "x": "1122", "y": "640" },
"p": { "x": "1122", "y": "666" },
"p": { "x": "1112", "y": "666" }
},
"bkcolor": "16777215",
"color": "0",
"code": "52",
"code_ascii": "4",
"confidence": "97"
},
"characater": {
"poly": {
"p": { "x": "1123", "y": "640" },
"p": { "x": "1132", "y": "640" },
"p": { "x": "1131", "y": "665" },
"p": { "x": "1123", "y": "665" }
},
"bkcolor": "16777215",
"color": "0",
"code": "53",
"code_ascii": "5",
"confidence": "97"
},
"characater": {
"poly": {
"p": { "x": "1134", "y": "640" },
"p": { "x": "1139", "y": "640" },
"p": { "x": "1139", "y": "664" },
"p": { "x": "1133", "y": "664" }
},
"bkcolor": "16777215",
"color": "0",
"code": "49",
"code_ascii": "1",
"confidence": "77"
},
"characater": {
"poly": {
"p": { "x": "1154", "y": "639" },
"p": { "x": "1163", "y": "639" },
"p": { "x": "1163", "y": "663" },
"p": { "x": "1153", "y": "663" }
},
"bkcolor": "16777215",
"color": "0",
"code": "66",
"code_ascii": "B",
"confidence": "97"
},
"characater": {
"poly": {
"p": { "x": "1164", "y": "638" },
"p": { "x": "1173", "y": "638" },
"p": { "x": "1173", "y": "663" },
"p": { "x": "1163", "y": "663" }
},
"bkcolor": "16777215",
"color": "0",
"code": "66",
"code_ascii": "B",
"confidence": "94"
},
"characater": {
"poly": {
"p": { "x": "1191", "y": "637" },
"p": { "x": "1206", "y": "636" },
"p": { "x": "1205", "y": "660" },
"p": { "x": "1190", "y": "661" }
},
"bkcolor": "16777215",
"color": "0",
"code": "76",
"code_ascii": "L",
"confidence": "34"
}
},
"det_time_us": "1104009",
"poly": {
"p": { "x": "1088", "y": "642" },
"p": { "x": "1210", "y": "634" },
"p": { "x": "1210", "y": "661" },
"p": { "x": "1087", "y": "669" }
}
}
},
"det_time_us": "1710270"
}
}
}
我有类似的东西:
$ jq -r '.frames.frame.lps.lp|.characters.characater.code_ascii,.characters.characater.confidence' test.json
但它只返回一个字母 & 它是 conf. 分数....
问题
- 如何取回所有字母和相关分数?
输出
我希望输出如下:
1 97, 5 89, 4 97,5 97, 1 77,B 97, B 94, L 34, J, 57,J 57, 4 7, 1 97, 5 89, 4 97, 5 97, 1 77, B 97, B 94, L 34
注意:格式可以不同,这只是表示我要提取的数据。
复制粘贴
输入文件“test.json”
{"response":{"container":{"id":"41d6efcb-24d6-490d-8880-762255519b5f","timestamp":"2018-Jul-11 19:51:06.461665"},"id":"00000002-0000-0000-0000-000000000015"},"frames":{"frame":{"id":"5583","timestamp":"2016-Nov-30 13:05:27","lps":{"lp":{"licenseplate":"15451BBL","text":"15451BBL","wtext":"15451BBL","confidence":"20","bkcolor":"16777215","color":"16777215","type":"0","ntip":"11","cct_country_short":"","cct_state_short":"","tips":{"tip":{"poly":{"p":{"x":"1094","y":"643"},"p":{"x":"1099","y":"643"},"p":{"x":"1099","y":"667"},"p":{"x":"1094","y":"667"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"97"},"tip":{"poly":{"p":{"x":"1103","y":"642"},"p":{"x":"1113","y":"642"},"p":{"x":"1112","y":"667"},"p":{"x":"1102","y":"667"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"89"},"tip":{"poly":{"p":{"x":"1112","y":"640"},"p":{"x":"1122","y":"640"},"p":{"x":"1122","y":"666"},"p":{"x":"1112","y":"666"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"97"},"tip":{"poly":{"p":{"x":"1123","y":"640"},"p":{"x":"1132","y":"640"},"p":{"x":"1131","y":"665"},"p":{"x":"1123","y":"665"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"97"},"tip":{"poly":{"p":{"x":"1134","y":"640"},"p":{"x":"1139","y":"640"},"p":{"x":"1139","y":"664"},"p":{"x":"1133","y":"664"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"77"},"tip":{"poly":{"p":{"x":"1154","y":"639"},"p":{"x":"1163","y":"639"},"p":{"x":"1163","y":"663"},"p":{"x":"1153","y":"663"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"97"},"tip":{"poly":{"p":{"x":"1164","y":"638"},"p":{"x":"1173","y":"638"},"p":{"x":"1173","y":"663"},"p":{"x":"1163","y":"663"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"94"},"tip":{"poly":{"p":{"x":"1191","y":"637"},"p":{"x":"1206","y":"636"},"p":{"x":"1205","y":"660"},"p":{"x":"1190","y":"661"}},"bkcolor":"16777215","color":"0","code":"76","code_ascii":"L","confidence":"34"},"tip":{"poly":{"p":{"x":"1103","y":"655"},"p":{"x":"1111","y":"655"},"p":{"x":"1111","y":"667"},"p":{"x":"1103","y":"667"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"57"},"tip":{"poly":{"p":{"x":"1103","y":"655"},"p":{"x":"1111","y":"655"},"p":{"x":"1111","y":"667"},"p":{"x":"1103","y":"667"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"57"},"tip":{"poly":{"p":{"x":"1176","y":"638"},"p":{"x":"1185","y":"637"},"p":{"x":"1184","y":"661"},"p":{"x":"1175","y":"662"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"7"}},"ncharacter":"8","characters":{"characater":{"poly":{"p":{"x":"1094","y":"643"},"p":{"x":"1099","y":"643"},"p":{"x":"1099","y":"667"},"p":{"x":"1094","y":"667"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"97"},"characater":{"poly":{"p":{"x":"1103","y":"642"},"p":{"x":"1113","y":"642"},"p":{"x":"1112","y":"667"},"p":{"x":"1102","y":"667"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"89"},"characater":{"poly":{"p":{"x":"1112","y":"640"},"p":{"x":"1122","y":"640"},"p":{"x":"1122","y":"666"},"p":{"x":"1112","y":"666"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"97"},"characater":{"poly":{"p":{"x":"1123","y":"640"},"p":{"x":"1132","y":"640"},"p":{"x":"1131","y":"665"},"p":{"x":"1123","y":"665"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"97"},"characater":{"poly":{"p":{"x":"1134","y":"640"},"p":{"x":"1139","y":"640"},"p":{"x":"1139","y":"664"},"p":{"x":"1133","y":"664"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"77"},"characater":{"poly":{"p":{"x":"1154","y":"639"},"p":{"x":"1163","y":"639"},"p":{"x":"1163","y":"663"},"p":{"x":"1153","y":"663"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"97"},"characater":{"poly":{"p":{"x":"1164","y":"638"},"p":{"x":"1173","y":"638"},"p":{"x":"1173","y":"663"},"p":{"x":"1163","y":"663"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"94"},"characater":{"poly":{"p":{"x":"1191","y":"637"},"p":{"x":"1206","y":"636"},"p":{"x":"1205","y":"660"},"p":{"x":"1190","y":"661"}},"bkcolor":"16777215","color":"0","code":"76","code_ascii":"L","confidence":"34"}},"det_time_us":"1072592","poly":{"p":{"x":"1088","y":"642"},"p":{"x":"1210","y":"634"},"p":{"x":"1210","y":"661"},"p":{"x":"1087","y":"669"}}}},"det_time_us":"1720812"}}}
关联
输入文件:https ://drive.google.com/file/d/18wCzjMBpw7SIeVFByAGPQiqCBjg_0te3/view?usp=sharing
这是一个新的和改进的答案。旧答案在最后的分隔符之后可用。
数据的问题在于它包含一个对象 ,
.frame.frames.lps.lp.characters
具有非唯一键 (characater
)。每次为已经存在的键提供值时,旧值都会丢失。We can work around this with
jq
by using the utility's "stream" input (--stream
):This first truncates the input stream at depth 5 (the length of the path
.frame.frames.lps.lp.characters
), and we extract any sub-path that starts with the problematiccharacater
key. This is what the innertruncate_stream()
and theselect()
are doing. The top level of every such sub-path is then truncated, and what remains is reconstituted into a stream of objects. This is what the outertruncate_stream()
is doing together thefromstream()
call that wraps it.At this point in the processing, the modified document looks like the following (given the example in the question):
For each object from
fromstream
, the data is extracted into an array formatted for output with@tsv
, giving us the tab-delimited output above.Old answer from 2018:
Since your
characters
array is not a proper JSON array (it's a hash where each key has the same name), only the last value of it will be available.We can fix that by preprocessing the data to create unique keys for each object that is now called
characater
:This would replace each
characater
withchar1
,char2
etc.We can now access all values in that with e.g.
The complete pipeline:
Result (given the data in the question):
If you control the generation of the JSON document, you should consider turning the
characters
object into a proper array.鉴于您的输入不是有效的 JSON,您可能必须使用
sed
,awk
,grep
类型的解决方案。为此,以下内容可以处理被“缩小”为单个字符串的输入:例子
备择方案
注意:这些适用于多行输入数据。
sed+粘贴 sed,awk,粘贴 sed,grep,粘贴 grep+粘贴grep + sed + tr :
返回 :
此命令仅返回 code_ascii + confidence 对:
返回 :
现在,请参阅上面的 Kusalananda 的帖子。只需 sed + tr :
返回 :