2017-02-07 101 views
-1

我只是想找到一種方法來搜索與JQ用shell腳本,讓更好的解釋:Shell腳本更復雜的JQ選擇

我有此JSON文件:

{ 
     "meta": { 
     "limit": 200, 
     "next": null, 
     "offset": 0, 
     "previous": null, 
     "total_count": 2 
     }, 
     "objects": [ 
     { 
      "bandwidth": 768, 
      "call_direction": "in", 
      "call_uuid": "84e6098a-d0a9-44ed-846e-074b6d563cfb", 
      "conference": "JOG1_VMR6", 
      "connect_time": "2017-01-26T19:20:01.096940", 
      "destination_alias": "[email protected]", 
      "display_name": "JG - Sala 2", 
      "encryption": "On", 
      "has_media": true, 
      "id": "92dab287-0091-4d57-bdff-f37cce6c586e", 
      "is_muted": false, 
      "is_on_hold": false, 
      "is_presentation_supported": true, 
      "is_presenting": false, 
      "is_streaming": false, 
      "license_count": 1, 
      "license_type": "port", 
      "media_node": "192.168.20.11", 
      "parent_id": "", 
      "participant_alias": "h323:192.168.51.153", 
      "protocol": "H323", 
      "remote_address": "192.168.51.153", 
      "remote_port": 11000, 
      "resource_uri": "/api/admin/status/v1/participant/92dab287-0091-4d57-bdff-f37cce6c586e/", 
      "role": "chair", 
      "service_tag": "JOG1", 
      "service_type": "conference", 
      "signalling_node": "192.168.20.11", 
      "source_alias": "h323:192.168.51.153", 
      "system_location": "CUSTOMER-JG-LAN", 
      "vendor": "TANDBERG (Tandberg 529)" 
     }, 
     { 
      "bandwidth": 1280, 
      "call_direction": "in", 
      "call_uuid": "dd60c9a2-22e0-4685-9a3d-8573e5e6cc75", 
      "conference": "Sala_Teste-Turn-up", 
      "connect_time": "2017-01-27T01:42:11.103894", 
      "destination_alias": "5001", 
      "display_name": "John", 
      "encryption": "On", 
      "has_media": true, 
      "id": "dd60c9a2-22e0-4685-9a3d-8573e5e6cc75", 
      "is_muted": false, 
      "is_on_hold": false, 
      "is_presentation_supported": false, 
      "is_presenting": false, 
      "is_streaming": false, 
      "license_count": 1, 
      "license_type": "port", 
      "media_node": "172.24.25.106", 
      "parent_id": "", 
      "participant_alias": "John", 
      "protocol": "WebRTC", 
      "remote_address": "179.65.15.9", 
      "remote_port": 62794, 
      "resource_uri": "/api/admin/status/v1/participant/dd60c9a2-22e0-4685-9a3d-8573e5e6cc75/", 
      "role": "chair", 
      "service_tag": "JOG", 
      "service_type": "conference", 
      "signalling_node": "172.24.25.106", 
      "source_alias": "John", 
      "vendor": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" 
     } 
     ] 
    } 

我希望能夠找到「會議」JSON TAG中的一個元素,當我找到會議名稱時,我想知道如何在找到會議值後再帶上「connect_time」:

這裏是一個例子,我的意思是: conference「:」JOG1_VMR6「

我想找到什麼是「CONNECT_TIME」在此特別是解析器,所以我建波紋管這部分代碼:

time=$(cat service.html 2>/dev/null | jq '.objects[] | select(.conference=='JOG1_VMR6')' | jq ".connect_time" | grep -o "[^\"]*" | grep -o "[^T][0-9].*" | grep -o "[0-9]\{2\}:[0-9]\{2\}:[0-9].") 

但選擇(.conference ==「JOG1_VMR6」)」顯示的是編譯錯誤,並且不會給我帶來「connect_time」:「2017-01-26T19:20:01.096940」,因此我無法正確解析它。

+2

這是不是有效的JSON文件。 'objects'中的列表缺少關閉']',並且關閉'}'太多。 – larsks

+0

對不起,朋友是因爲我從文件中複製過來的,它只是文件的一部分,請注意完整的文件列表(超過200個對象)。 – Daniel

+0

好吧現在已經被糾正了。 – Daniel

回答

0

這裏成功的關鍵是簡單。 JQ的只是一個調用:

jq '.objects[] | select(.conference=="JOG1_VMR6") | .connect_time' 

所有這些grep調用也可以大大簡化,或者完全消除 - 考慮,例如:

jq -n -r '"2017-01-26T19:20:01.096940" | sub(".*T(?<t>..:..:..).*"; .t)' 
19:20:01 

或者只是:

jq -n -r '"2017-01-26T19:20:01.096940" | .[11:19]' 
19:20:01 
+0

它就像一個魅力!謝謝Peak – Daniel

+0

這是非常正確的,但它不能解釋爲什麼舊的嘗試(在jq腳本中使用與周圍相同的引號)失敗。 –

+0

我這樣做是因爲它是「JOG1_VMR6」它是一個完整的數組。 time = $(cat amazon.html 2>/dev/null | jq'.objects [] | select(.conference =='$ {compare1 [$ i]}')| jq .connect_time'| grep -o 「[grep -o」[0-9] \ {2 \}:[0-9] \ {2 \ }:[0-9]。「) – Daniel