2016-09-19 90 views
1

我有一個JSON對象的流文件如下:猛砸有JQ分組

{"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 ,網址和文件名:

這是我到目前爲止有:

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 
+0

更好運行'jq ...

+0

這對於'jq'來說並不重要,但是對sort分類很重要 - 如果它有一個真正的文件句柄,那麼'sort'的優化實現可以使子進程尋求不同的部分文件,並行排序組件,然後在父進程中進行合併排序;使用不可尋址的FIFO進行輸入時,如果沒有不可並行的初始讀取通道,則無法進行這種優化,因爲輸入流只能從前到後只讀取一次。 –

回答

0

有了一個小小的例外,你可以很容易地實現使用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)) 
+0

看起來像是我在找什麼。但是像這樣運行它: cat /tmp/file.json | 〜/ bin_compciv/jq -scr'map({id,url,file})| GROUP_BY(.file)| 。[] | ((地圖(.id)| @csv),(地圖(.url)| @csv))' 它說-scr不被識別。嘗試沒有-scr,出現錯誤:「jq:錯誤:無法用字符串索引數組 jq:錯誤:數組在csv行無效」 – OkyDokyman

+0

顯然,您沒有使用最新版本的jq(1.5)。如果您使用1.4版,請使用「-s -c -r」而不是-scr。如果您使用1.3,則可能需要其他更改,但最好升級到v1.4。 – peak

0

下面是使用GROUP_BY-r-s JQ選擇一個解決方案:

group_by(.file)[] 

| ([ "\(.[].id)" ] | join(",")), 
    ([ .[].url  ] | join(",")), 
    .[0].file