从日志文件中提取json数据

时间:2018-07-05 03:40:58

标签: json bash shell

我有一个日志文件,其中包含“随机”内容和json字符串。我想要的是,从json字符串中提取某些变量/值对,但是我不确定该怎么做。例如,我有类似的东西:

...logs...
...logs...
...logs... {"response":{"container":{"id":"ca78183e-248a-44d7-89fe-f4080f0ec5fa","timestamp":"2018-Jun-30 18:01:57.928633"},"id":"00000002-0000-0000-0000-00000000003f"},"frames":{"frame":{"id":"33139","timestamp":"2016-Nov-30 13:51:29.200000","lps":{"lp":{"licenseplate":"49239BA","text":"49239BA","wtext":"49239BA","confidence":"77","bkcolor":"16777215","color":"16777215","type":"540073","ntip":"7","cct_country_short":"USA","cct_state_short":"NY","tips":{"tip":{"poly":{"p":{"x":"960","y":"438"},"p":{"x":"968","y":"438"},"p":{"x":"968","y":"460"},"p":{"x":"960","y":"460"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"98"},"tip":{"poly":{"p":{"x":"971","y":"437"},"p":{"x":"979","y":"437"},"p":{"x":"979","y":"460"},"p":{"x":"971","y":"460"}},"bkcolor":"16777215","color":"0","code":"57","code_ascii":"9","confidence":"94"},"tip":{"poly":{"p":{"x":"980","y":"436"},"p":{"x":"988","y":"436"},"p":{"x":"988","y":"460"},"p":{"x":"980","y":"460"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"98"},"tip":{"poly":{"p":{"x":"990","y":"436"},"p":{"x":"998","y":"436"},"p":{"x":"998","y":"458"},"p":{"x":"990","y":"459"}},"bkcolor":"16777215","color":"0","code":"51","code_ascii":"3","confidence":"95"},"tip":{"poly":{"p":{"x":"1000","y":"435"},"p":{"x":"1008","y":"435"},"p":{"x":"1008","y":"458"},"p":{"x":"1000","y":"458"}},"bkcolor":"16777215","color":"0","code":"57","code_ascii":"9","confidence":"96"},"tip":{"poly":{"p":{"x":"1020","y":"434"},"p":{"x":"1029","y":"434"},"p":{"x":"1029","y":"458"},"p":{"x":"1020","y":"458"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"96"},"tip":{"poly":{"p":{"x":"1031","y":"435"},"p":{"x":"1039","y":"435"},"p":{"x":"1039","y":"457"},"p":{"x":"1031","y":"457"}},"bkcolor":"16777215","color":"0","code":"65","code_ascii":"A","confidence":"98"}},"ncharacter":"7","characters":{"characater":{"poly":{"p":{"x":"960","y":"438"},"p":{"x":"968","y":"438"},"p":{"x":"968","y":"460"},"p":{"x":"960","y":"460"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"98"},"characater":{"poly":{"p":{"x":"971","y":"437"},"p":{"x":"979","y":"437"},"p":{"x":"979","y":"460"},"p":{"x":"971","y":"460"}},"bkcolor":"16777215","color":"0","code":"57","code_ascii":"9","confidence":"94"},"characater":{"poly":{"p":{"x":"980","y":"436"},"p":{"x":"988","y":"436"},"p":{"x":"988","y":"460"},"p":{"x":"980","y":"460"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"98"},"characater":{"poly":{"p":{"x":"990","y":"436"},"p":{"x":"998","y":"436"},"p":{"x":"998","y":"458"},"p":{"x":"990","y":"459"}},"bkcolor":"16777215","color":"0","code":"51","code_ascii":"3","confidence":"95"},"characater":{"poly":{"p":{"x":"1000","y":"435"},"p":{"x":"1008","y":"435"},"p":{"x":"1008","y":"458"},"p":{"x":"1000","y":"458"}},"bkcolor":"16777215","color":"0","code":"57","code_ascii":"9","confidence":"96"},"characater":{"poly":{"p":{"x":"1020","y":"434"},"p":{"x":"1029","y":"434"},"p":{"x":"1029","y":"458"},"p":{"x":"1020","y":"458"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"96"},"characater":{"poly":{"p":{"x":"1031","y":"435"},"p":{"x":"1039","y":"435"},"p":{"x":"1039","y":"457"},"p":{"x":"1031","y":"457"}},"bkcolor":"16777215","color":"0","code":"65","code_ascii":"A","confidence":"98"}},"det_time_us":"787294","poly":{"p":{"x":"953","y":"424"},"p":{"x":"1047","y":"420"},"p":{"x":"1047","y":"466"},"p":{"x":"953","y":"470"}}},"lp":{"licenseplate":"BLEBR1","text":"BLEBR1","wtext":"BLEBR1","confidence":"46","bkcolor":"16777215","color":"16777215","type":"0","ntip":"10","cct_country_short":"","cct_state_short":"","tips":{"tip":{"poly":{"p":{"x":"1222","y":"555"},"p":{"x":"1231","y":"554"},"p":{"x":"1230","y":"569"},"p":{"x":"1221","y":"569"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"94"},"tip":{"poly":{"p":{"x":"1232","y":"554"},"p":{"x":"1241","y":"554"},"p":{"x":"1240","y":"568"},"p":{"x":"1231","y":"569"}},"bkcolor":"16777215","color":"0","code":"76","code_ascii":"L","confidence":"76"},"tip":{"poly":{"p":{"x":"1254","y":"553"},"p":{"x":"1263","y":"553"},"p":{"x":"1262","y":"567"},"p":{"x":"1253","y":"567"}},"bkcolor":"16777215","color":"0","code":"69","code_ascii":"E","confidence":"88"},"tip":{"poly":{"p":{"x":"1270","y":"552"},"p":{"x":"1278","y":"551"},"p":{"x":"1278","y":"566"},"p":{"x":"1269","y":"566"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"90"},"tip":{"poly":{"p":{"x":"1286","y":"551"},"p":{"x":"1295","y":"551"},"p":{"x":"1294","y":"565"},"p":{"x":"1285","y":"565"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"95"},"tip":{"poly":{"p":{"x":"1297","y":"550"},"p":{"x":"1302","y":"550"},"p":{"x":"1301","y":"564"},"p":{"x":"1296","y":"564"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"85"},"tip":{"poly":{"p":{"x":"1240","y":"554"},"p":{"x":"1248","y":"553"},"p":{"x":"1247","y":"568"},"p":{"x":"1239","y":"568"}},"bkcolor":"16777215","color":"0","code":"90","code_ascii":"Z","confidence":"22"},"tip":{"poly":{"p":{"x":"1240","y":"554"},"p":{"x":"1248","y":"553"},"p":{"x":"1247","y":"568"},"p":{"x":"1239","y":"568"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"38"},"tip":{"poly":{"p":{"x":"1240","y":"554"},"p":{"x":"1248","y":"553"},"p":{"x":"1247","y":"568"},"p":{"x":"1239","y":"568"}},"bkcolor":"16777215","color":"0","code":"90","code_ascii":"Z","confidence":"22"},"tip":{"poly":{"p":{"x":"1240","y":"554"},"p":{"x":"1248","y":"553"},"p":{"x":"1247","y":"568"},"p":{"x":"1239","y":"568"}},"bkcolor":"16777215","color":"0","code":"65","code_ascii":"A","confidence":"1"}},"ncharacter":"6","characters":{"characater":{"poly":{"p":{"x":"1222","y":"555"},"p":{"x":"1231","y":"554"},"p":{"x":"1230","y":"569"},"p":{"x":"1221","y":"569"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"94"},"characater":{"poly":{"p":{"x":"1232","y":"554"},"p":{"x":"1241","y":"554"},"p":{"x":"1240","y":"568"},"p":{"x":"1231","y":"569"}},"bkcolor":"16777215","color":"0","code":"76","code_ascii":"L","confidence":"76"},"characater":{"poly":{"p":{"x":"1254","y":"553"},"p":{"x":"1263","y":"553"},"p":{"x":"1262","y":"567"},"p":{"x":"1253","y":"567"}},"bkcolor":"16777215","color":"0","code":"69","code_ascii":"E","confidence":"88"},"characater":{"poly":{"p":{"x":"1270","y":"552"},"p":{"x":"1278","y":"551"},"p":{"x":"1278","y":"566"},"p":{"x":"1269","y":"566"}},"bkcolor":"16777215","color":"0","code":"66","code_ascii":"B","confidence":"90"},"characater":{"poly":{"p":{"x":"1286","y":"551"},"p":{"x":"1295","y":"551"},"p":{"x":"1294","y":"565"},"p":{"x":"1285","y":"565"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"95"},"characater":{"poly":{"p":{"x":"1297","y":"550"},"p":{"x":"1302","y":"550"},"p":{"x":"1301","y":"564"},"p":{"x":"1296","y":"564"}},"bkcolor":"16777215","color":"0","code":"49","code_ascii":"1","confidence":"85"}},"det_time_us":"322188","poly":{"p":{"x":"1220","y":"555"},"p":{"x":"1306","y":"549"},"p":{"x":"1305","y":"565"},"p":{"x":"1219","y":"570"}}}},"det_time_us":"1673162"}}}
...logs...
...logs...
...logs... different json line
...logs...

并假设我想提取文件中每个json行的“牌照”和以下“信心”值。 如何在bash中做到最好?

2 个答案:

答案 0 :(得分:1)

您可以使用以下grep命令:

grep -o '"\(licenseplate\|confidence\)":"[^"]*"' inputfile

打印:

"licenseplate":"49239BA"
"confidence":"77"
"confidence":"98"
"confidence":"94"
...
"licenseplate":"BLEBR1"
"confidence":"46"
"confidence":"94"
"confidence":"76"
...

答案 1 :(得分:1)

您可以使用grep提取JSON数据并使用jq进行解析:

grep -o '{.*}' file | jq -r '.frames.frame.lps.lp | .licenseplate, .confidence'
BLEBR1
46

如果JSON数据为1行,这将起作用。由于正则表达式{.*}是贪婪的,因此grep将立即捕获整个JSON数据。

jq选择JSON结构frames.frame.lps.lp,从中提取值licenseplateconfidence。请注意-r以获取原始数据(不带双引号)。

如果您需要在同一行中同时使用这两个值(以进行进一步解析),请使用以下jq命令:

grep -o '{.*}' file | jq -r '.frames.frame.lps.lp | "\(.licenseplate) \(.confidence)"'
BLEBR1 46