Break jq query string into lines

放肆的年华 提交于 2020-12-26 05:24:47

问题


How can i break jq string into lines, this is for long lines, when i put "\" query breaks.

vpcExists=$(aws ec2 describe-vpcs --profile $profile | jq -r --arg vpcId "$vpcId" '.[][] | \
 select(.VpcId == $vpcId) \
| .["State"]' \
)

回答1:


jq is fine with literal line breaks, so just add linefeeds anywhere without trying to escape them:

vpcExists=$(aws ec2 describe-vpcs --profile $profile |
    jq -r --arg vpcId "$vpcId" '
   .[][] 
     | select(.VpcId == $vpcId)
     | .["State"]' 
)

Here's a MCVE:

jq -r --arg vpcId "someId" '
   .[][] 
     | select(.VpcId == $vpcId)
     | .["State"]'  << 'EOF'


{ "Vpcs": [ {
            "VpcId": "someId",
            "InstanceTenancy": "default",
            "State": "available",
            "IsDefault": false
        } ] }
EOF


来源:https://stackoverflow.com/questions/48712545/break-jq-query-string-into-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!