jq - 在匹配另一个属性时选择 1 属性

问题描述 投票:0回答:1

输入 JSON :

{
  "data": {
    "zone": [
      {
        "record": [
          {
            "Line": 1,
            "raw": "; cPanel first:11.25.0-CURRENT_46156 (update_time):1708598274 Cpanel::ZoneFile::VERSION:1.3 hostname:server2.something.com latest:110.0.23",
            "type": ":RAW"
          },
          {
            "Line": 4,
            "Lines": 1,
            "class": "IN",
            "expire": "1209600",
            "minimum": "86400",
            "mname": "ns1.something.com",
            "name": "domain.com.",
            "refresh": "3600",
            "retry": "1800",
            "rname": "noreply.something.com",
            "serial": "2024022220",
            "ttl": 86400,
            "type": "SOA"
          },
          {
            "Line": 6,
            "address": "195.216.197.40",
            "class": "IN",
            "name": "ftp.domain.com.",
            "ttl": 60,
            "type": "A"
          },
          {
            "Line": 97,
            "class": "IN",
            "cname": "0c960d0901ed2ec4ae18f39c5dcf547e.b3778944564d3daa444a04e58f3b1649.comodoca.com",
            "name": "_f0ff734bef4f3db24a0dc94fc6b1d21a.fab.domain.com.",
            "ttl": 60,
            "type": "CNAME"
          },
          {
            "Line": 114,
            "class": "IN",
            "cname": "eaf925d9c7f0fbe3e1abcaa94671918b.814242e76a38aea40755b96ee978a9fc.comodoca.com",
            "name": "_671f4707fe0a8b2443fc93fe3a7a7a1d.www.software.domain.com.",
            "ttl": 60,
            "type": "CNAME"
          }
        ]
      }
    ]
  },
  "metadata": {
    "command": "dumpzone",
    "reason": "Zone Serialized",
    "result": 1,
    "version": 1
  }
}

我想要获取的是“cname”包含或以“comodoca.com”结尾的“行”号。例如:

97
114

我花了很长时间才让它工作,所以最后我作弊并使用了 grep 和 awk。

jq --raw-output '.data.zone[].record[] | (.Line|tostring) + "," + .cname ' | grep -i 'comodoca.com' | awk -F, '{ print $1}'

有没有办法在 jq 中完成这一切?谢谢!

我想出的最接近可行的方法是:

jq --raw-output '.data.zone[].record[] | select(endswith("comodoca.com"))]'

工作,但没有。

json jq
1个回答
0
投票

…包含…

data.zone[].record[]
| select(.cname | tostring | test("comodoca.com")).Line
© www.soinside.com 2019 - 2024. All rights reserved.