jq 不打印 select() returns null 的行

jq is not printing the lines where select() returns null

我正在尝试使用带标签名称的 awscli 从 AWS 获取所有 ec2 实例。
我使用的命令是

aws ec2 describe-instances | \
jq -r '.Reservations[].Instances[] | (.Tags[] | select(.Key == "Name") | .Value) + " " \
+ .InstanceId + " " + .InstanceType + " " + .KeyName + " " + .PrivateIpAddress + " " + .PublicIpAddress'

这里jq只打印有tag Name的实例,没有tag name的实例不打印。
我是否以错误的方式使用 select()?

实际输出:

master-bastion i-026b52da57ae3a85 t2.micro aus 10.90.0.68 52.62.76.17
master-mongodb_1 i-083bceea3aea1832 t3.medium aus 10.90.100.25
master-mongo i-06d669ba5cda0c74 t3.medium aus 10.90.100.12
master-solr1 i-09752d54fe143fec t2.medium aus 10.90.100.12
master-solr2 i-039bf2028ec15d97 t2.medium aus 10.90.101.22
master-solr3 i-09fc04ceeeb1efae t2.medium aus 10.90.100.6
rabbitmq-1 i-0125de65ba60627a t2.small aus 10.90.100.10
rabbitmq-2 i-069d546deb4a1c23 t2.small aus 10.90.101.11

预期输出:

master-bastion i-026b52da57ae3a85 t2.micro aus 10.90.0.68 52.62.76.17
i-06d669ba5cda0c4d t3.medium aus 10.90.100.142
i-062669ba5cda0sfs t3.medium aus 10.90.100.147
master-mongodb_1 i-083bceea3aea1832 t3.medium aus 10.90.100.25
master-mongo i-06d669ba5cda0c74 t3.medium aus 10.90.100.12
master-solr1 i-09752d54fe143fec t2.medium aus 10.90.100.12
master-solr2 i-039bf2028ec15d97 t2.medium aus 10.90.101.22
master-solr3 i-09fc04ceeeb1efae t2.medium aus 10.90.100.6
rabbitmq-1 i-0125de65ba60627a t2.small aus 10.90.100.10
rabbitmq-2 i-069d546deb4a1c23 t2.small aus 10.90.101.11

如果我们只关注标签,我看到两个解决方案:

(.Tags[] | select(.Key == "Name")).value // ""

这使用 // 运算符来 return "" 以防 .value 不存在。

或者,您可以先将标签硬塞到一个对象中:

.Tags | map({key: .Key, value: .Value}) | from_entries | .Name // ""