Ho questo JSON e voglio ottenere l'id della subnet corrispondente che si adatta alla subnet variabile.ricerca tipo regex in un json con jq
subnet="192.168.112"
json='{
"subnets": [
{
"cidr": "192.168.112.0/24",
"id": "123"
},
{
"cidr": "10.120.47.0/24",
"id": "456"
}
]
}'
Poiché regex non è supportato con jq. L'unico modo che ho trovato per ottenere il giusto id è a mixte grep, sed e JQ come questo:
tabNum=$((`echo ${json} | jq ".subnets[].cidr" | grep -n "$subnet" | sed "s/^\([0-9]\+\):.*$/\1/"` - 1))
NET_ID=`echo ${json} | jq -r ".subnets[${tabNum}].id"`
C'è un modo per ottenere l'id solo con JQ?
Le espressioni regolari sono supportate in jq 1.5. Potresti usare quello. –