我有一个包含 100000 个结果的大 JSON 文件。我知道如何使用 'JQ'
进行基本解析。
病毒.json
{
"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 单独获取它。
我试过了,
1
$ jq -r ".detected" virus.json
true
true
true
2
$ jq -r ".result" dum_1.json
Trojan.Win32.Generic!BT
FileCryptor.NJX
Generic.Ransom.Purge.DC87C66E
3
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)"'
关于json - 使用 JQ Issue 解析复杂的 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42479789/