将带有过滤器的 AWS CLI 命令输出到 CSV 而不使用 jq [英] Output AWS CLI command with filters to CSV without jq
问题描述
有没有办法将带有过滤器的 AWS cli 输出为 csv 格式?
Is there a way to output the AWS cli with filters to csv format?
例如,如果我想将此命令输出到 csv:
For example if I wanted to take this command and output to csv:
aws ec2 describe-images --owner self --query 'Images[*].{ID:ImageId,"Virtualization Type":VirtualizationType}'
我该怎么做?通常我会使用 jq 将 aws cli 输出到 csv.但在这种情况下,我可以使用过滤器选项而不是 jq 更轻松地获得我想要的信息.
How would I do that? Normally I would use jq to output the aws cli to csv. But in this case I was able to get to the info I wanted easier with the filter option instead of jq.
这是我想输出到 CSV 的完整命令:
This is the full command I want to output to CSV:
aws ec2 describe-images --owner self --query 'Images[*].{ID:ImageId,"Virtualization Type":VirtualizationType,Architechture:Architecture,Hypervisor:Hypervisor,State:State,ImageID:ImageId,"Device Names":BlockDeviceMappings[].DeviceName,"Snapshot IDs":BlockDeviceMappings[].Ebs.SnapshotId,"Delete On Termination":BlockDeviceMappings[].Ebs.DeleteOnTermination,"Voluem Type":BlockDeviceMappings[].Ebs.VolumeType,"Volume Size":BlockDeviceMappings[].Ebs.VolumeSize,Encrypted:BlockDeviceMappings[].Ebs.Encrypted,"Image Location":ImageLocation,"Root Device Type":RootDeviceType,"Owner ID":OwnerId,"Creation Date":CreationDate,Public:Public,"Image Type":ImageType,Name:Name}'
推荐答案
我能想到的一个解决方案是在文本中输出 --output text
然后用逗号替换空格:>
One solution I can think of is to output in text --output text
and then replace the spaces with a comma:
aws ec2 describe-images --owner self --query 'Images[*].{ID:ImageId,"Virtualization Type":VirtualizationType}' --output text
输出
ami-1234567890 hvm
ami-1a2b3c4d5e hvm
ami-9876543210 hvm
用逗号替换空格.有很多方法可以使用 sed
或 tr
或 awk
或 paste
来做到这一点.
Replace the blanks with a comma. There are many ways to do this using sed
or tr
or awk
or paste
.
aws ec2 describe-images --owner self --query 'Images[*].{ID:ImageId,"Virtualization Type":VirtualizationType}' --output text | sed -E 's/\s+/,/g'
输出
ami-1234567890,hvm
ami-1a2b3c4d5e,hvm
ami-9876543210,hvm
这篇关于将带有过滤器的 AWS CLI 命令输出到 CSV 而不使用 jq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!