Search code examples
amazon-web-servicescommand-line-interfaceaws-cli

AWS CLI query return on one line


I have the following CLI command in a shell file. I can't figure out how to get the output to come out all on 1 line:

aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value[], PrivateIpAddress]' --output text

The output comes out like this

instance1
10.10.1.10
instance2
10.10.1.11

instead of:

instance1 10.10.1.10
instance2 10.10.1.11

I know the solution has something to do with the placement of the "[]"s. I tried moving and removing them in various ways.

How can I get my output to line up?


Solution

  • The newline is a result of the Tags being an array that can have multiple values.

    You can avoid the newline by specifying that you only want the first entry in the array:

    aws ec2 describe-instances --query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0], PrivateIpAddress]' --output text
    

    The result is:

    one 172.30.0.19
    two 172.30.1.210