如何在jq

时间:2019-09-23 13:48:17

标签: jq

我不喜欢原始的inspect方法来搜索Docker上的特定字段,因此我使用jq查找所需的配置,例如:

docker inspect test | jq '.[].NetworkSettings
docker inspect test | jq '.. | .NetworkSettings? | objects'

两个脚本都可以得到结果:

{
  "Bridge": "",
  "SandboxID": "4951989910db53c8bb8368add9ddcdf0e69cd14a9ff09ef95402850f24c5de08",
  "HairpinMode": false,
  "LinkLocalIPv6Address": "",
  "LinkLocalIPv6PrefixLen": 0,
  "Ports": {},
  "SandboxKey": "/var/run/docker/netns/4951989910db",
  "SecondaryIPAddresses": null,
  "SecondaryIPv6Addresses": null,
  "EndpointID": "",
  "Gateway": "",
  "GlobalIPv6Address": "",
  "GlobalIPv6PrefixLen": 0,
  "IPAddress": "",
  "IPPrefixLen": 0,
  "IPv6Gateway": "",
  "MacAddress": "",
  "Networks": {
    "test_default": {
      "IPAMConfig": null,
      "Links": null,
      "Aliases": [
        "f3df83d51385",
        "test"
      ],
      "NetworkID": "4b09f5c3a82cb7c9770efec1fc818eb071cf707bd60ec96202664d5282d73b6c",
      "EndpointID": "",
      "Gateway": "",
      "IPAddress": "",
      "IPPrefixLen": 0,
      "IPv6Gateway": "",
      "GlobalIPv6Address": "",
      "GlobalIPv6PrefixLen": 0,
      "MacAddress": "",
      "DriverOpts": null
    }
  }
}

但是基于我完全正确输入了配置名称NetworkSettings的情况,有没有一种方法可以递归地执行模糊搜索关键字并且jq不区分大小写?像docker inspect test | jq '.. | search(. | match("network";"i") | objects'一样,它不起作用,只是一个例子。

1 个答案:

答案 0 :(得分:2)

具有以下定义:

def blurry($f):
  ($f | ascii_upcase) as $ucf
  | to_entries[]
  | select((.key|ascii_upcase) == $ucf)
  | .value;

您可以写:

.. | blurry("NetworkSettings")? | objects

模糊器

模糊不清,您可以走得更远,例如:

def veryBlurry($f):
  ($f | ascii_upcase) as $ucf
  | to_entries[]
  | select(.key | ascii_upcase | index($ucf))
  | .value;

您可以沿着这条路径继续,例如使用正则表达式。