0

I am using the command below aws ec2 describe-instances to return list of running EC2s. The output is a TSV in 2 lines, first line being Account ID (OwnerId), and the rest of the query is displayed at the second line. I would like to make all the attributes to be flattened in one line, but due to the nature of the json output of the API calls, is there a way to manipulate the output to be in just one line?

aws ec2 describe-instances --output text --profile $account --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running >> $outfile

1234567890 i-03cxxxxxxxdab t2.medium running windows ap-southeast-1a 10.0.0.0 10.10.0.10 api-abc-prod-01 PROD

desired output in one line

1234567890 i-03cxxxxxxxdab t2.medium running windows ap-southeast-1a 10.0.0.0 10.10.0.10 api-abc-prod-01 PROD

user2155404
  • 133
  • 4
  • 13

2 Answers2

1

As recommended in the top answer in Parsing JSON with Unix tools --> you can use jq to do this https://stedolan.github.io/jq/, check the tutorial here on extracting the relevent fields... https://stedolan.github.io/jq/tutorial/

Jorg Roper
  • 617
  • 1
  • 5
  • 12
1

First thing, AWS cli offer to specify the output formate, so you can set the output to text then replace the new line with space. you can try

aws ec2 describe-instances --output text  --profile test --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]'  --filter --filters Name=instance-state-name,Values=running --output text | tr '\r\n' ' '

Text Output Format

The text format organizes the AWS CLI's output into tab-delimited lines. It works well with traditional Unix text tools such as grep, sed, and awk, as well as the text processing performed by PowerShell.

The text output format follows the basic structure shown below. The columns are sorted alphabetically by the corresponding key names of the underlying JSON object.

IDENTIFIER  sorted-column1 sorted-column2
IDENTIFIER2 sorted-column1 sorted-column2

so you are good to go with pipe using tr '\r\n' ' '

Or you can use awk

aws ec2 describe-instances --output text  --profile test --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]'  --filter --filters Name=instance-state-name,Values=running  | awk 1 ORS=' '

update:

If you want to append ownerID with each instance details then use this.

aws ec2 describe-instances --output text   --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]'  --filter --filters Name=instance-state-name,Values=running | paste -d" " - -

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • I have tried both `tr '\r\n' ' '` and `awk 1 ORS=' '` it returned the output in a long single line – user2155404 Oct 11 '19 at 08:07
  • You mentioned that you need in one line? – Adiii Oct 11 '19 at 08:50
  • what i meant was, each instance displayed in one line, eg. instance 1 ownerid publicipaddress privateipaddress name \nInstance 2 ownerid publicipaddress privateipaddress – user2155404 Oct 11 '19 at 09:10
  • that works greatly, thank you! I corrected it slightly `aws ec2 describe-instances --output text --query 'Reservations[*].[[OwnerId, Instances[*].[InstanceId, InstanceType, State.Name, Platform, Placement.AvailabilityZone, PublicIpAddress, PrivateIpAddress,[Tags[?Key==Name].Value][0][0],[Tags[?Key==Environment].Value][0][0]]]]' --filter --filters Name=instance-state-name,Values=running | paste -d"\t" - -` – user2155404 Oct 11 '19 at 15:39