aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

Preserve the order of keys in the output table

Open justindho opened this issue 3 years ago • 6 comments

Describe the feature

Currently, when performing a query and setting the output format to 'table', the order of the query keys is not preserved. For example, we can see in this example that the order the query keys (Name, ImageId, CreationDate) differs from the order of the column headers in the output table (CreationDate, ImageId, Name).

$ aws ec2 describe-images --owner amazon --filters 'Name=name,Values=amzn-ami-*-x86_64-gp2' --query "Images[?CreationDate > \`$(date --date='2 month ago' +%Y-%m-%d)\`].{Name:Name,ImageId:ImageId,CreationDate:CreationDate}" --output table --color off
-------------------------------------------------------------------------------------------------------
|                                           DescribeImages                                            |
+---------------------------+------------------------+------------------------------------------------+
|       CreationDate        |        ImageId         |                     Name                       |
+---------------------------+------------------------+------------------------------------------------+
|  2020-09-04T02:10:36.000Z |  ami-021234069a5506633 |  amzn-ami-hvm-2018.03.0.20200904.0-x86_64-gp2  |
|  2020-08-11T20:09:50.000Z |  ami-034dad9bdb65ec34b |  amzn-ami-hvm-2018.03.0.20200729.0-x86_64-gp2  |
+---------------------------+------------------------+------------------------------------------------+

The feature would add support for preserving the order of keys specified in a query in the output table. For example, if the keys in the query are specified in order of Name, ImageId, and CreationDate, the order of the columns in the output table would be Name, ImageId, and CreationDate.

$ aws ec2 describe-images --owner amazon --filters 'Name=name,Values=amzn-ami-*-x86_64-gp2' --query "Images[?CreationDate > \`$(date --date='2 month ago' +%Y-%m-%d)\`].{Name:Name,ImageId:ImageId,CreationDate:CreationDate}" --output table --color off

-------------------------------------------------------------------------------------------------------
|                                           DescribeImages                                            |
+-----------------------------------------------+------------------------+----------------------------+
|                     Name                      |        ImageId         |       CreationDate         |
+-----------------------------------------------+------------------------+----------------------------+
|  amzn-ami-hvm-2018.03.0.20200904.0-x86_64-gp2 |  ami-021234069a5506633 |  2020-09-04T02:10:36.000Z  |
|  amzn-ami-hvm-2018.03.0.20200729.0-x86_64-gp2 |  ami-034dad9bdb65ec34b |  2020-08-11T20:09:50.000Z  |
+-----------------------------------------------+------------------------+----------------------------+

Use Case

When running commands with --output table, the output should be easier to read. Having the output columns rendered in the order that I've specified in the query would help readability.

Proposed Solution

#5581

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

CLI version used

any

Environment details (OS name and version, etc.)

any

justindho avatar May 11 '22 23:05 justindho

I run into the same problem with --output text. It's very frustrating.

Farmbuyer avatar May 14 '22 01:05 Farmbuyer

This is very much needed. It's amazing that this is not the default behavior. Furthermore when it IS the actual behavior for --output json but not --output table where it is by alphabetical order. Why this discrepancy @justindho ?

adamency avatar Jun 17 '22 23:06 adamency

I need this feature badly as well. Is there any new on this?

znitsch avatar Jan 13 '23 22:01 znitsch

It should be legit if this was a thing.

CyDickey-msr avatar Jun 06 '23 20:06 CyDickey-msr

Would really like to see this as an option as well. Currently have to get creative with names, or prefix them with digits as an ugly hack/option.

sgrotten avatar Apr 05 '24 18:04 sgrotten

It's not ideal but you can fake it by producing an array of arrays and stick an ersatz column heading on with literals, e.g.

aws ec2 describe-instances \
  --query '[
    [[`Name`, `ID`, `AZ`, `Roles`, `State`]],
    Reservations[*].Instances[*].[Tags[?Key==`Name`]|[0].Value,InstanceId,Placement.AvailabilityZone,Tags[?Key==`Roles`]|[0].Value,State.Name][] | sort_by(@, &[0])
  ][]' \
  --output table --color off

however generally I'll reach for better tools at this point e.g. jq if available.

inopinatus avatar May 11 '24 02:05 inopinatus