Tom Donnly
Tom Donnly

Reputation: 29

jq select field based on contains from another field

I am trying to pull out of this list the ip_prefix based on where region contains us-west- and list them in csv. When I try jq '.prefixes[] | .ip_prefix,.region | select(contains("us")?)' file.json I only get a list of regions.

{
  "syncToken": "15985471",
  "createDate": "2021-10-08-20-13-16",
  "prefixes": [
    {
      "ip_prefix": "3.5.140.0/22",
      "region": "ap-northeast-2",
      "service": "AMAZON",
      "network_border_group": "ap-northeast-2"
    },
    {
      "ip_prefix": "13.34.37.64/27",
      "region": "ap-southeast-4",
      "service": "AMAZON",
      "network_border_group": "ap-southeast-4"
    },
    {
      "ip_prefix": "35.180.0.0/16",
      "region": "eu-west-3",
      "service": "AMAZON",
      "network_border_group": "eu-west-3"
    },
    {
      "ip_prefix": "52.93.153.170/32",
      "region": "eu-west-2",
      "service": "AMAZON",
      "network_border_group": "eu-west-2"
    },
    {
      "ip_prefix": "52.93.178.234/32",
      "region": "us-west-1",
      "service": "AMAZON",
      "network_border_group": "us-west-1"
    }
  ]
}

Upvotes: 1

Views: 3151

Answers (1)

Jarrod Baker
Jarrod Baker

Reputation: 1220

You have the filters in the wrong order. Try:

jq '.prefixes[] | select(.region|startswith("us-west")) | .ip_prefix' <your json file> 

What this is doing is getting the prefixes array, filtering for elements where the region starts with "us-west" and then selecting the ip_prefix.

Upvotes: 2

Related Questions