2

I am extracting some data from my AWS account using AWS cli. I am not able to extract the exact string. When I am giving the following command: -

aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[ [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`Operating System`].Value] [0][0], [Tags[?Key==`Environment`].Value] [0][0], InstanceId, InstanceType,VpcId, State.Name, PrivateIpAddress, PublicIpAddress]
'| column -t | grep production

I am getting the output as

 sftp_gateway_instance        rhel     production  i-0aec9xxxxxxxxxxxd  t2.xlarge   vpc-zzzzzz1b  running  2.3.4.5    3.2.1.2
 abc-production-nav-1         rhel     production  i-0e4xxxxxxxxxxxxx3  t2.xlarge   vpc-zzzzzz1b  running  1.1.6.5    None
 xyz-produ-nav                rhel     production  i-0xxxxxxxxxxxxxx18  t2.xlarge   vpc-zzzzzz1b  running  2.8.0.4    None
 solutions-production-navi    centos   uat         i-08fffffffffff86c8  t2.large    vpc-zzzzzz1b  running  2.8.9.2    None

In the output provided above I don't want the last row because the production is mentioned in the name of instance not in the environment column.

I tried below awk command but in that case the output in not coming in proper column format

aws ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[ [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`Operating System`].Value] [0][0], [Tags[?Key==`Environment`].Value] [0][0], InstanceId, InstanceType,VpcId, State.Name, PrivateIpAddress, PublicIpAddress]
'| awk -F ' ' '$3 == "production" {print $1 "\t\t" $2 "\t" $3"\t" $4"\t" $5"\t" $6"\t" $7"\t" $8"\t" $9 }' 

The output in this case is like below:-

abc-production  centos  production  i-xyzabcdef t2.xlarge   vpc-xyzabc  running 2.18.1.0    None
doc-pdf-prod windows production i-xyzabcdef t2.large    vpc-xyzabc  running 172.18.70.229   None

I want the output in proper column format like the above output which i am getting using grep. Could anyone please suggest what can be done for this?

1

Instead of grep production, which matches any line with "production" within it, use awk '$3 == "production"', which only matches lines in which the third field is "production".

No need for anything further within the awk : by default it will then print the entire matching line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.