繁体   English   中英

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
.
.
.

但是- 我想按文件名对它们进行分组,这样我就可以在同一行上得到逗号分隔的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM