Home > OS >  How to use wildcards to filter values using jq based on starting names
How to use wildcards to filter values using jq based on starting names

Time:09-06

Can someone please help me to filter multiple values based on starting names from below json output. I am able to filter values same type attributes but not others in same query.

{
  "app1.btch_src": {
    "eventrate": 0,
    "end2end": {
      "data": [
        {
          "at": 16054343444,
          "source": "app1.btch_src",
          "latency": 0.05,
          "target": "app1.btch_tgt"
        }
      ]
    }
  },
  "app1.btch1_src": {
    "eventrate": 0,
    "end2end": {
      "data": [
        {
          "at": 16054343445,
          "source": "app1.btch1_src",
          "latency": 200.5,
          "target": "app1.btch1_tgt"
        }
      ]
    }
  },
  "app2.btch_src": {
    "eventrate": 0,
    "end2end": {
      "data": [
        {
          "at": 16054341234,
          "source": "app2.btch_src",
          "latency": 50.4,
          "target": "app2.btch_tgt"
        }
      ]
    }
  },
  "app3.btch_src": {
    "eventrate": 0,
    "end2end": {
      "data": [
        {
          "at": 16054343444,
          "source": "app3.btch_src",
          "latency": 10,
          "target": "app3.btch_tgt"
        }
      ]
    }
  }
}

I've written query like below as I want to retrieve both app1.* and app2.* object values if latency is more than 10 sec. when I select below one attribute and it's working.

jq -r '.[] |.end2end.data[0] | select(.latency>10) |select(.source |startswith("app1")' all_info.json

I need to know now how I can retrieve the app2 values along with app1 if above condition satisfied.

CodePudding user response:

You could simply or the conditions, e.g. using your approach:

.[] 
| .end2end.data[0]
| select(.latency>10)
| select(.source | (startswith("app1") or startswith("app2")) )

Or you could use a regular expression, e.g.

select( .source | test("^app[12]") )

or perhaps

select( .source | test("^app[12][.]") )
  • Related