Shell脚本更复杂的jq选择

时间:2017-02-07 12:50:19

标签: json shell sh jq

我只想找到一种使用shellcript搜索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": "9892@192.168.20.11",
          "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”:

这是一个例子,我的意思是: 会议“:”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].")

但是select(.conference =='JOG1_VMR6')'在shell中显示编译错误,不会给我带来“connect_time”:“2017-01-26T19:20:01.096940”所以我不能解析它是正确的。

1 个答案:

答案 0 :(得分:1)

这里成功的关键是简单。只需调用一次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