jq解析多个值

时间:2019-09-05 18:31:37

标签: json bash jq

我正在尝试从同时具有

的JSON输出displayName
"source": "0.0.0.0/0" and
tcpOptions": "destinationPortRange": "min": 80

结果应仅显示

rule-1

例如:JSON

[
  {
    "displayName": "rule-1",
    "secrule": [
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 80,
            "max": 80
          }
        }
      },
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 443,
            "max": 443
          }
        }
      }
    ]
  },
  {
    "displayName": "rule-2",
    "secrule": [
      {
        "source": "0.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 443,
            "max": 443
          }
        }
      },
      {
        "source": "20.0.0.0/0",
        "tcpOptions": {
          "destinationPortRange": {
            "min": 80,
            "max": 80
          }
        }
      }
    ]
  }
]

我尝试过

jq -r '.[] | select(.secrule[].source == "0.0.0.0/0" and .secrule[].tcpOptions.destinationPortRange.min == 80) | .displayName' JSON | sort -u

但是它同时显示两个规则(不正确)

rule-1
rule-2

1 个答案:

答案 0 :(得分:2)

您要扩展display: contain两次,因此将检查其元素的每个组合。请改用.grid { display: flex; flex-wrap: wrap; } .grid > div { display: contents; } .grid aside { order: 1; } .grid aside + aside { order: 3; } .grid main { order: 2; } .grid section { order: 4; } .grid main, .grid section, .grid aside { width: 100%; } /* Set grid layout for screens larger than 700px only */ @media (min-width: 700px) { .grid { display: grid; grid-template-columns: 1fr 1fr; grid-column-gap: 2em; } .grid > div { display: block; } /* Add a rule between the aside and the main content */ .grid > div + div { padding-left: 2em; border-left: 1px solid #aaa; } /* Set order back to 0 */ .grid section, .grid main, .grid aside { order: unset; } }

<section class="grid">
  <div>
    <main>
      <h1>Main content</h1>
      <p>Your main content can go here...</p>
    </main>

    <section>
      <h1>Secondary content</h1>
      <p>Your secondary content can go here...</p>
    </section>
  </div>
  
  <div>
    <aside>
      <p>Complementary 1</p>
      <p>Complementary 1</p>
      <p>Complementary 1</p>
      <p>Complementary 1</p>
      <p>Complementary 1</p>
      <p>Complementary 1</p>
    </aside>

    <aside>
      <p>Complementary 2</p>
    </aside>
  </div>
</section>