Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve the order of keys in the output table #6941

Open
2 tasks
justindho opened this issue May 11, 2022 · 6 comments · May be fixed by #5581
Open
2 tasks

Preserve the order of keys in the output table #6941

justindho opened this issue May 11, 2022 · 6 comments · May be fixed by #5581
Labels
feature-request A feature should be added or improved. p2 This is a standard priority issue

Comments

@justindho
Copy link
Contributor

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 justindho added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels May 11, 2022
@justindho justindho linked a pull request May 11, 2022 that will close this issue
@justindho justindho removed the needs-triage This issue or PR still needs to be triaged. label May 11, 2022
@Farmbuyer
Copy link

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

@justindho justindho linked a pull request May 20, 2022 that will close this issue
@adamency
Copy link

adamency commented Jun 17, 2022

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 ?

@tim-finnigan tim-finnigan added the p2 This is a standard priority issue label Nov 10, 2022
@znitsch
Copy link

znitsch commented Jan 13, 2023

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

@CyDickey-msr
Copy link

It should be legit if this was a thing.

@sgrotten
Copy link

sgrotten commented Apr 5, 2024

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.

@inopinatus
Copy link

inopinatus commented May 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved. p2 This is a standard priority issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants