简体   繁体   中英

Parsing AWS CLI commands

I want to parse the output of AWS CLI command

aws ec2 describe-transit-gateways

在此处输入图像描述

在此处输入图像描述

From the above, the output I want to get is the name of the transit gateway which is given as a tag example: ("Name": "dev-tgw")

I am able to retrieve TGW ID and Transit Gateway Owner from the below script

result=`aws ec2 describe-transit-gateways --profile $acc`

  for tgw in $(echo "${result}" |jq -r '.TransitGateways[] | @base64'); do

    _jq() {

      echo ${tgw} | base64 --decode | jq -r ${1}

    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags.Name')

    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"

But I am not able to retrieve the Name of the TGW using

 Tgw_name=$(_jq '.Tags.Name')
 echo "TGW_name": "$Tgw_name"

What am I missing or am I looping it wrong?

You can try the following version:

result=$(aws ec2 describe-transit-gateways --profile $acc)

for tgw in $(echo "${result}" | jq -r '.TransitGateways[] | @base64'); do

    _jq() {
      echo ${tgw} | base64 --decode | jq -r "${1}"
    }
    
    Tgw=$(_jq '.TransitGatewayId')
    Tgw_owner=$(_jq '.OwnerId')
    Tgw_name=$(_jq '.Tags[] | select(.Key == "Name").Value')
    
    echo "$acc.Name"
    echo "Transit_Gateway": "$Tgw"
    echo "TGW_Owner_ID": "$Tgw_owner"
    echo "TGW_name": "$Tgw_name"
    
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM