Bash用JQ分组
Bash with JQ grouping
我有一个包含 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、URL 和文件名各一行:
这是我目前拥有的:
cat /tmp/file.json | ~/bin_compciv/jq --raw-output '.id,.url,.file'
结果:
4496
http://xxx/yyy
/home/build/branches/mmm/file1
.
.
.
BUT - 我想按文件名对它们进行分组,这样我就可以在同一行获得以逗号分隔的 url 和 id 列表,如下所示:
4496,4497
http://xxx/yyy,http://xxx/yyy/zzz
/home/build/branches/mmm/file1
除了一个小例外,您可以按如下方式使用 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
的另一个调用;或如下所述。但是,如果任何 URL 有可能包含逗号,这可能不是一个好主意。
下面是仅调用一次 jq 即可消除引号的过滤器:
map({id,url,file})
| group_by(.file)
| .[]
| ((map(.id) | @csv),
([map(.url) | join(",")] | @csv | .[1:-1]),
(.[0] | .file))
这是一个使用 group_by 和 -r
、-s
jq 选项的解决方案:
group_by(.file)[]
| ([ "\(.[].id)" ] | join(",")),
([ .[].url ] | join(",")),
.[0].file
我有一个包含 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、URL 和文件名各一行:
这是我目前拥有的:
cat /tmp/file.json | ~/bin_compciv/jq --raw-output '.id,.url,.file'
结果:
4496
http://xxx/yyy
/home/build/branches/mmm/file1
.
.
.
BUT - 我想按文件名对它们进行分组,这样我就可以在同一行获得以逗号分隔的 url 和 id 列表,如下所示:
4496,4497
http://xxx/yyy,http://xxx/yyy/zzz
/home/build/branches/mmm/file1
除了一个小例外,您可以按如下方式使用 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
的另一个调用;或如下所述。但是,如果任何 URL 有可能包含逗号,这可能不是一个好主意。
下面是仅调用一次 jq 即可消除引号的过滤器:
map({id,url,file})
| group_by(.file)
| .[]
| ((map(.id) | @csv),
([map(.url) | join(",")] | @csv | .[1:-1]),
(.[0] | .file))
这是一个使用 group_by 和 -r
、-s
jq 选项的解决方案:
group_by(.file)[]
| ([ "\(.[].id)" ] | join(",")),
([ .[].url ] | join(",")),
.[0].file