Linux
如何 txt -> json 轉換?
C-11^Mark^Docking AWS C-12^Mark^Docking AWS C-13^Lay^Ploting C-14^Lay^Ploting C-15^Lay^Ploting C-16^Vincent^GPU Calc C-17^Vincent^GPU Calc C-18^Vincent^GPU Calc C-19^Vincent^GPU Calc
我使用這個命令:
jq -Rn ' [inputs | select(. != "") / "^"] | reduce .[] as [$container, $user, $job] ({}; .[$job] += [{$user, $container}] ) 'test.csv > test.json
我想要這種格式的數據
{ "job": "Docking AWS", "user": "Mark", "container": [ "C-11", "C-12" ] }
. . .
我怎樣才能做到這一點?
這個問題最初是對另一個問題的調整,我提供了一個答案。我還在那裡解決了調整後的問題。
我想出的shell程式碼如下:
jq -Rn ' reduce ( inputs / "^" ) as [$container,$user,$job] ({}; .[$job] |= ( .job |= $job | .user |= $user | .container += [ $container ] ) ) | [ .[] ]' file
這用於
reduce
將對象創建為頂級對像中的子對象,每個對像都由作業字元串鍵入。當所有數據都以這種方式處理後,子對象將擴展為頂級數組。鑑於問題中的數據,這會生成以下 JSON 文件:
[ { "job": "Docking AWS", "user": "Mark", "container": [ "C-11", "C-12" ] }, { "job": "Ploting", "user": "Lay", "container": [ "C-13", "C-14", "C-15" ] }, { "job": "GPU Calc", "user": "Vincent", "container": [ "C-16", "C-17", "C-18", "C-19" ] } ]