[英]Parsing Complex JSON file with JQ Issue
我有大的JSON文件,有100000個結果。 我知道如何使用'JQ'
進行基本解析。
{
"detected": true,
"result": "Trojan.Win32.Generic!BT",
"update": "20170115",
"version": "1.5.0.42"
}
{
"detected": true,
"result": "FileCryptor.NJX",
"update": "20170115",
"version": "16.0.0.4749"
}
{
"detected": true,
"result": "Generic.Ransom.Purge.DC87C66E",
"update": "20170115",
"version": "1.0.1.9"
}
但是在這個JSON文件中,我希望以CSV
格式獲取“檢測到”和“結果”等字段。 我知道如何使用JQ單獨獲取它。
我試過了 ,
$ jq -r ".detected" virus.json
true
true
true
$ jq -r ".result" dum_1.json
Trojan.Win32.Generic!BT
FileCryptor.NJX
Generic.Ransom.Purge.DC87C66E
jq -r ".detected,.result" dum_1.json
true
Trojan.Win32.Generic!BT
true
FileCryptor.NJX
true
Generic.Ransom.Purge.DC87C66E
而不是#3,我希望輸出為
true , Trojan.Win32.Generic!BT
true , FileCryptor.NJX
true , Generic.Ransom.Purge.DC87C66E
有關如何獲得結果的任何建議?
@csv將從一個平面數組轉換為CSV,所以這應該讓你開始:
jq -r '[.detected, .result] | @csv'
根據您的樣本輸入,這將產生:
true,"Trojan.Win32.Generic!BT"
true,"FileCryptor.NJX"
true,"Generic.Ransom.Purge.DC87C66E"
如果要刪除引號,請考慮:
jq -r '"\(.detected), \(.result)"'
你可以使用sed
:
jq -r ".detected,.result" dum_1.json | sed 'N;s/\n/ , /'
true , Trojan.Win32.Generic!BT
true , FileCryptor.NJX
true , Generic.Ransom.Purge.DC87C66E
sed
N
命令讀取下一行,將兩行連接在一起。
該s
命令正在取代換行符\\n
與想要的圖案,
。
jq -r ".detected,.result" dum_1.json |awk -v OFS=, 'NR%2{x=$0;next} {print x OFS $0}'
true,Trojan.Win32.Generic!BT
true,FileCryptor.NJX
true,Generic.Ransom.Purge.DC87C66E
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.