Post

How to use regexp with jq

How to use regexp within jq when selecting documents.

How to use regexp within jq when selecting documents.

The test() function in jq matches a string against a regular expression. Combined with --arg to pass shell variables into jq, this lets you dynamically filter GCP project lists by name patterns. This is useful when you need to find projects matching a specific naming convention.

1
2
3
4
5
6
PROJECT_NAMES="one|two|there"
REGEXP_SOL_PROJ="^prefix-${ENVIRONMENT}-(${PROJECT_NAMES}).*"
REGION="europe-west3"

HSM_PROJ=$(gcloud projects --format=json list | jq -r --arg RGEXP "^eaut-${ENVIRONMENT}-hsm-dap-kernel.*$" '.[] | select(.projectId|test($RGEXP)) | .projectId')

This post is licensed under CC BY 4.0 by the author.