我有一个带有JSON对象流的文件,如下所示:
{"id":4496,"status":"Analyze","severity":"Critical","severityCode":1,"state":"New","code":"RNPD.DEREF","title":"Suspicious dereference of pointer before NULL check","message":"Suspicious dereference of pointer \u0027peer-\u003esctSapCb\u0027 before NULL check at line 516","file":"/home/build/branches/mmm/file1","method":"CzUiCztGpReq","owner":"unowned","taxonomyName":"C and C++","dateOriginated":1473991086512,"url":"http://xxx/yyy","issueIds":[4494]}
{"id":4497,"status":"Analyze","severity":"Critical","severityCode":1,"state":"New","code":"NPD.GEN.CALL.MIGHT","title":"Null pointer may be passed to function that may dereference it","message":"Null pointer \u0027tmpEncodedPdu\u0027 that comes from line 346 may be passed to function and can be dereferenced there by passing argument 1 to function \u0027SCpyMsgMsgF\u0027 at line 537.","file":"/home/build/branches/mmm/file1","method":"CzUiCztGpReq","owner":"unowned","taxonomyName":"C and C++","dateOriginated":1473991086512,"url":"http://xxx/yyy/zzz","issueIds":[4495]}
{"id":4498,"status":"Analyze","severity":"Critical","severityCode":1,"state":"New","code":"NPD.GEN.CALL.MIGHT","title":"Null pointer may be passed to function that may dereference it","message":"Null pointer \u0027tmpEncodedPdu\u0027 that comes from line 346 may be passed to function and can be dereferenced there by passing argument 1 to function \u0027SCpyMsgMsgF\u0027 at line 537.","file":"/home/build/branches/mmm/otherfile.c","method":"CzUiCztGpReq","owner":"unowned","taxonomyName":"C and C++","dateOriginated":1473991086512,"url":"http://xxx/yyy/zzz","issueIds":[4495]}
我想使用JQ(或其他方式),三行,每个ID,ID和文件名各一行:
这是我到目前为止所做的:
cat /tmp/file.json | ~/bin_compciv/jq --raw-output '.id,.url,.file'
结果:
4496
http://xxx/yyy
/home/build/branches/mmm/file1
.
.
.
但是 - 我想按文件名对它们进行分组,这样我就可以在同一行上获得以逗号分隔的网址和ID列表,如下所示:
4496,4497
http://xxx/yyy,http://xxx/yyy/zzz
/home/build/branches/mmm/file1
答案 0 :(得分:1)
除了一个小例外,您可以使用jq轻松实现所述目标:
jq -scr 'map({id,url,file})
| group_by(.file)
| .[]
| ((map(.id) | @csv) , (map(.url) | @csv), (.[0] | .file))'
根据您的输入,输出将为:
4496,4497
"http://xxx/yyy","http://xxx/yyy/zzz"
/home/build/branches/mmm/file1
4498
"http://xxx/yyy/zzz"
/home/build/branches/mmm/otherfile.c
然后,您可以使用文本编辑工具(例如sed
)删除引号;使用另一个jq
的调用;或如下所述。但是,如果任何网址都包含逗号,那么这可能不是一个好主意。
这里是仅使用一次jq调用来消除引号的过滤器:
map({id,url,file})
| group_by(.file)
| .[]
| ((map(.id) | @csv),
([map(.url) | join(",")] | @csv | .[1:-1]),
(.[0] | .file))
答案 1 :(得分:0)
以下是使用 group_by 和-r
,-s
jq选项的解决方案:
group_by(.file)[]
| ([ "\(.[].id)" ] | join(",")),
([ .[].url ] | join(",")),
.[0].file