Home > OS >  jq bash issue with filtering with CONTAINS
jq bash issue with filtering with CONTAINS

Time:11-15

I have an issue where I am trying to filter records with a CONTAINS, but it won't accept a variable that has spaces in it. I am including the JSON and the calls. I explain what works and the last one that does not work. I have looked High and Low but I can't make it work. I have seen and tried many (hundreds of ways taking into account the double quotes, escaped, not escaped, with, without, but no luck) can someone take a look and point me to something that might help.

JSON used to test

_metadatadashjson='{  "meta": {    "provisionedExternalId": ""  },  "dashboard": {    "liveNow": false,    "panels": [      {        "collapsed": false,        "title": "Gyrex Thread Count Gauges",        "type": "row", "targets": [          {            "expr": "jvm_threads_current{instance=\"192.1.50.22:8055\",job=\"prometheus_gyrex\"}",            "refId": "B"          }        ]      },      {        "datasource": "Prometheus_16_Docker",        "targets": [          {            "exemplar": true,            "expr": "jvm_threads_current{instance=\"10.32.0.4:8055\",job=\"prometheus_gyrex\"}"          }        ],        "title": ".16 : 3279",        "type": "gauge"      },      {        "description": "",        "targets": [          {            "expr": "jvm_threads_current{instance=\"10.32.0.7:8055\",job=\"prometheus_gyrex\"}",            "refId": "B"          }        ],        "title": ".16 : 3288",        "type": "graph"      },      {        "description": "",        "targets": [          {            "expr": "jvm_threads_current{instance=\"192.168.2.16:3288\",job=\"prometheus_gyrex\"}",            "refId": "C"          }        ],        "title": ".16 : 3288",        "type": "graph"      }    ],    "version": 55  }}'

Set the string to search for in key "expr"

exprStrSearch="10.32.0.4:8055"

This works returns one record

echo "${_metadatadashjson}" | jq -r --arg EXPRSTRSEARCH "$exprStrSearch" '.dashboard.panels[] | select(.targets[].expr | contains($EXPRSTRSEARCH)) | .targets[].expr'

This works no problem returns two records.

echo "${_metadatadashjson}" | jq -r --arg EXPRSTRSEARCH "$exprStrSearch" '.dashboard.panels[] | select(.targets[].expr | contains("10.32.0.4:8055", "10.32.0.7:8055")) | .targets[].expr'

Change the value to include a space and another string

exprStrSearch="10.32.0.4:8055 10.32.0.7:8055"

Does not work.

echo "${_metadatadashjson}" | jq -r --arg EXPRSTRSEARCH "$exprStrSearch" '.dashboard.panels[] | select(.targets[].expr | contains($EXPRSTRSEARCH)) | .targets[].expr'

CodePudding user response:

The problem is that the string with the space in it does not in fact occur in the given JSON. It's not too clear what you are trying to do but please note that contains is not symmetric:

"a" | contains("a b")

evaluates to false.

If you intended to write a boolean search criterion, you could use a boolean expression, or use jq's regular expression machinery, e.g.

test("10.32.0.4:8055|10.32.0.7:8055")

or probably even better:

test("\"(10.32.0.4:8055|10.32.0.7:8055)\"")

CodePudding user response:

None of your data contains "10.32.0.4:8055 10.32.0.7:8055".

You could pass multiple strings to contains(), using a bash array:

strings=("10.32.0.4:8055" "10.32.0.7:8055")

echo "${_metadatadashjson}" |
jq -r --args '.dashboard.panels[] | select(.targets[].expr | contains($ARGS.positional[])) | .targets[].expr' "${strings[@]}"

But contains will evaluate to true for each match. Ie. if one expr contained both strings, it would be selected (and printed) twice.

With test, that won't happen. Here's how you can add the |s between multiple strings, and pass them in a single jq variable:

strings=("10.32.0.4:8055" "10.32.0.7:8055")
IFS=\|
echo "${_metadatadashjson}" |
jq -r --arg str "${strings[*]}" '.dashboard.panels[] | select(.targets[].expr | test($str)) | .targets[].expr'

Both examples print this:

jvm_threads_current{instance="10.32.0.4:8055",job="prometheus_gyrex"}
jvm_threads_current{instance="10.32.0.7:8055",job="prometheus_gyrex"}
  • Related