かなり長いJSON行(〜1.5KB)が複数あります。おそらく次のようになります。
"XYZ":{"myString":"myValue",ABCD}
行全体から「myString」:「myValue」を抽出したいと思います。 grep自体はすべて1行にあり、grep
toの出力をパイプしたいのですが、awk
区切り文字が必要に応じて動作しないようですので、私には役立ちません。誰でも助けることができますか?
この文書は単なるJSON文書ではありません。ファイルには多くのコンテンツがあり、その一部はmyValue(myStringの一部として)を抽出したいJSON行です。例は次のとおりです。
....other content...
....other content...
....other content...
....other content...!: {"response":{"container":{"id":"655ccc89-915c-4aae-a927-cca34bbedc29","timestamp":"2018-May-30
21:05:29.789543"},"id":"00000002-0000-0000-0000-000000000003"},"frames":{"frame":
{"id":"585","timestamp":"2016-Nov-30 12:57:06.500000","lps":{"lp":{"myString":"myValue","text":"8578D","wtext":"8578D","confidence":"4","bkcolor":"16777215","color":"16777215","type":"550171","ntip":"6","cct_country_short":"USA","cct_state_short":"FL","tips":{"tip":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"3"},"tip":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"4"},"tip":{"poly":{"p":{"x":"450","y":"28"},"p":{"x":"468","y":"28"},"p":{"x":"467","y":"63"},"p":{"x":"449","y":"63"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"98"},"tip":{"poly":{"p":{"x":"471","y":"28"},"p":{"x":"489","y":"28"},"p":{"x":"488","y":"63"},"p":{"x":"470","y":"63"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"tip":{"poly":{"p":{"x":"495","y":"28"},"p":{"x":"512","y":"28"},"p":{"x":"511","y":"63"},"p":{"x":"494","y":"63"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"98"},"tip":{"poly":{"p":{"x":"516","y":"28"},"p":{"x":"534","y":"28"},"p":{"x":"533","y":"63"},"p":{"x":"515","y":"63"}},"bkcolor":"16777215","color":"0","code":"68","code_ascii":"D","confidence":"98"}},"ncharacter":"5","characters":{"characater":{"poly":{"p":{"x":"425","y":"28"},"p":{"x":"442","y":"28"},"p":{"x":"441","y":"62"},"p":{"x":"425","y":"62"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"4"},"characater":{"poly":{"p":{"x":"450","y":"28"},"p":{"x":"468","y":"28"},"p":{"x":"467","y":"63"},"p":{"x":"449","y":"63"}},"bkcolor":"16777215","color":"0","code":"53","code_ascii":"5","confidence":"98"},"characater":{"poly":{"p":{"x":"471","y":"28"},"p":{"x":"489","y":"28"},"p":{"x":"488","y":"63"},"p":{"x":"470","y":"63"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"characater":{"poly":{"p":{"x":"495","y":"28"},"p":{"x":"512","y":"28"},"p":{"x":"511","y":"63"},"p":{"x":"494","y":"63"}},"bkcolor":"16777215","color":"0","code":"56","code_ascii":"8","confidence":"98"},"characater":{"poly":{"p":{"x":"516","y":"28"},"p":{"x":"534","y":"28"},"p":{"x":"533","y":"63"},"p":{"x":"515","y":"63"}},"bkcolor":"16777215","color":"0","code":"68","code_ascii":"D","confidence":"98"}},"det_time_us":"230387","poly":{"p":{"x":"353","y":"8"},"p":{"x":"541","y":"8"},"p":{"x":"540","y":"77"},"p":{"x":"352","y":"78"}}},"lp":{"licenseplate":"FRJ724","text":"FRJ724","wtext":"FRJ724","confidence":"66","bkcolor":"16777215","color":"16777215","type":"540122","ntip":"6","cct_country_short":"USA","cct_state_short":"NY","tips":{"tip":{"poly":{"p":{"x":"1553","y":"247"},"p":{"x":"1560","y":"247"},"p":{"x":"1560","y":"267"},"p":{"x":"1553","y":"267"}},"bkcolor":"16777215","color":"0","code":"70","code_ascii":"F","confidence":"94"},"tip":{"poly":{"p":{"x":"1561","y":"248"},"p":{"x":"1568","y":"248"},"p":{"x":"1568","y":"267"},"p":{"x":"1561","y":"267"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"99"},"tip":{"poly":{"p":{"x":"1570","y":"248"},"p":{"x":"1575","y":"248"},"p":{"x":"1575","y":"267"},"p":{"x":"1570","y":"267"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"84"},"tip":{"poly":{"p":{"x":"1586","y":"247"},"p":{"x":"1592","y":"247"},"p":{"x":"1592","y":"267"},"p":{"x":"1586","y":"267"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"tip":{"poly":{"p":{"x":"1593","y":"248"},"p":{"x":"1600","y":"248"},"p":{"x":"1600","y":"267"},"p":{"x":"1593","y":"267"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"90"},"tip":{"
poly":{"p":{"x":"1602","y":"248"},"p":{"x":"1607","y":"248"},"p":{"x":"1607","y":"266"},"p":{"x":"1602","y":"266"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"95"}},"ncharacter":"6","characters":{"characater":{"poly":{"p":{"x":"1553","y":"247"},"p":{"x":"1560","y":"247"},"p":{"x":"1560","y":"267"},"p":{"x":"1553","y":"267"}},"bkcolor":"16777215","color":"0","code":"70","code_ascii":"F","confidence":"94"},"characater":{"poly":{"p":{"x":"1561","y":"248"},"p":{"x":"1568","y":"248"},"p":{"x":"1568","y":"267"},"p":{"x":"1561","y":"267"}},"bkcolor":"16777215","color":"0","code":"82","code_ascii":"R","confidence":"99"},"characater":{"poly":{"p":{"x":"1570","y":"248"},"p":{"x":"1575","y":"248"},"p":{"x":"1575","y":"267"},"p":{"x":"1570","y":"267"}},"bkcolor":"16777215","color":"0","code":"74","code_ascii":"J","confidence":"84"},"characater":{"poly":{"p":{"x":"1586","y":"247"},"p":{"x":"1592","y":"247"},"p":{"x":"1592","y":"267"},"p":{"x":"1586","y":"267"}},"bkcolor":"16777215","color":"0","code":"55","code_ascii":"7","confidence":"99"},"characater":{"poly":{"p":{"x":"1593","y":"248"},"p":{"x":"1600","y":"248"},"p":{"x":"1600","y":"267"},"p":{"x":"1593","y":"267"}},"bkcolor":"16777215","color":"0","code":"50","code_ascii":"2","confidence":"90"},"characater":{"poly":{"p":{"x":"1602","y":"248"},"p":{"x":"1607","y":"248"},"p":{"x":"1607","y":"266"},"p":{"x":"1602","y":"266"}},"bkcolor":"16777215","color":"0","code":"52","code_ascii":"4","confidence":"95"}},"det_time_us":"672351","poly":{"p":{"x":"1543","y":"236"},"p":{"x":"1618","y":"237"},"p":{"x":"1618","y":"275"},"p":{"x":"1543","y":"274"}}}},"det_time_us":"1703430"}}}
....other content...
....other content...
....other content...
答え1
myString
文字列がファイル内にあり、完全な形式のJSON文書を形成すると仮定すると、次のコマンドは文書内のすべてのキーに対応する値を抽出します。
jq -r '..|select(type == "object" and has("myString")).myString' file.json
この式は、ドキュメントのキーを持つJSONオブジェクトをjq
再帰的に選択し、そのキーの値を抽出します。myString