【问题标题】:JQ : Change some values and display all the datas?JQ:更改一些值并显示所有数据?
【发布时间】:2021-01-14 14:17:19
【问题描述】:

我有这些数据:

{
   "cidr" : "X.X.X.X/27",
   "defaultGateway" : "X.X.X.X",
   "full" : false,
   "id" : "X.X.X.X",
   "ipAddressTab" : [
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the network address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "network"
      },
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the default gateway address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "gateway"
      },
      {
         "alias_domain" : "toto.com",
         "alias_name" : "",
         "description" : "this is a test",
         "dns_domain" : "",
         "environnement" : "test",
         "fdqn" : "XXX",
         "hostname" : "XXX",
         "ip" : "X.X.X.X",
         "requester" : "XXX",
         "status" : "allocated",
         "type" : "VM"
      },
      {
         "alias_domain" : "toto.com",
         "alias_name" : "",
         "description" : "this is a test",
         "dns_domain" : "",
         "environnement" : "test",
         "fdqn" : "XXX",
         "hostname" : "XXX",
         "ip" : "X.X.X.X",
         "requester" : "XXX",
         "status" : "allocated",
         "type" : "VM"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "reserved"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "reserved"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "reserved"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "reserved"
      },
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the broadcast address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "broadcast"
      }
   ]
}

我想将所有状态“保留”更改为“可用”,而不更改具有网络、网关或广播状态的 ip 的状态。因此,使用 jq 我可以选择所有我需要的内容,而无需选择具有网络、网关或广播的 IP 作为状态并将状态更改为可用:

cat myfile |  jq '.ipAddressTab[] | select(.status == "reserved") | select(.type != "network") | select(.type != "gateway") | select(.type != "broadcast") | .status = "available"' 

输出:

{
  "ip": "X.X.X.X",
  "status": "available"
}
{
  "ip": "X.X.X.X",
  "status": "available"
}
{
  "ip": "X.X.X.X",
  "status": "available"
}
{
  "ip": "X.X.X.X",
  "status": "available"
}

但是有一种方法可以做到这一点,以便获得此输出:

{
   "cidr" : "X.X.X.X/27",
   "defaultGateway" : "X.X.X.X",
   "full" : false,
   "id" : "X.X.X.X",
   "ipAddressTab" : [
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the network address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "network"
      },
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the default gateway address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "gateway"
      },
      {
         "alias_domain" : "toto.com",
         "alias_name" : "",
         "description" : "this is a test",
         "dns_domain" : "",
         "environnement" : "test",
         "fdqn" : "XXX",
         "hostname" : "XXX",
         "ip" : "X.X.X.X",
         "requester" : "XXX",
         "status" : "allocated",
         "type" : "VM"
      },
      {
         "alias_domain" : "toto.com",
         "alias_name" : "",
         "description" : "this is a test",
         "dns_domain" : "",
         "environnement" : "test",
         "fdqn" : "XXX",
         "hostname" : "XXX",
         "ip" : "X.X.X.X",
         "requester" : "XXX",
         "status" : "allocated",
         "type" : "VM"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "available"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "available"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "available"
      },
      {
         "ip" : "X.X.X.X",
         "status" : "available"
      },
      {
         "alias_domain" : null,
         "alias_name" : null,
         "description" : "This is the broadcast address for X.X.X.X/27",
         "dnr_rr" : null,
         "dns_domain" : null,
         "environnement" : null,
         "fdqn" : null,
         "hostname" : null,
         "ip" : "X.X.X.X",
         "requester" : null,
         "status" : "reserved",
         "type" : "broadcast"
      }
   ]
}

显示所有需要更改的数据,而不仅仅是显示应该更改的行?

【问题讨论】:

    标签: json linux parsing jq cat


    【解决方案1】:

    对于这类问题,|= 是你的朋友。

    您可以选择直接的方法,例如:

    .ipAddressTab
     |= map(if .status == "reserved" 
               and .type != "network"
               and .type != "gateway"
               and .type != "broadcast"
            then .status = "available" else . end)
    

    或更微妙的:

    (.ipAddressTab[]
     | select(.status == "reserved" and 
              .type != "network" and
              .type != "gateway" and
              .type != "broadcast")
    ).status = "available"
    

    顺便说一句,任何一种方法的 DRYer 版本都可以通过使用 IN 获得:

    .type | IN("network", "gateway", "broadcast") | not
    

    【讨论】:

    • 您好,非常感谢!我发现或多或少相同的东西.ipAddressTab[] | if .type != "network" and .type != "gateway" and .type != "broadcast" then .status = "available" else . end谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多