0

I am using the Amazon Cli tools to write an ec2 audit script.I want to fetch the tags like name, owner, cost centre from the ec2-describe-instances.

I am using this command :

ec2-describe-instances | grep -i "tag" | grep -i -e "name" -e "owner" -e "cost.centre"

please help

19
  • it is giving data in a paragraph not formatted way Commented Mar 26, 2014 at 4:50
  • Update your question to show whats your expected output? Commented Mar 26, 2014 at 4:50
  • yeah see or i want to to have headinga like instance id, name, owner etx then there are value Commented Mar 26, 2014 at 4:53
  • 2
    You first ask the output to come in single line and now you are asking for headings at top and values on bottom. I'm confused as to what you want as output. Clearly specify your requirement in your question. Post sample data that covers different scenarios. Are some tags missing Names or Owners? Commented Mar 26, 2014 at 5:29
  • 1
    possible duplicate of How to fetch the tags for ec2-describe-instances in a shell script
    – BMW
    Commented Mar 26, 2014 at 9:07

1 Answer 1

1

You can pipe your existing command to awk for formatting ...

ec2-describe-instances | grep -i "tag" | grep -i -e "name" -e "owner" -e "cost.centre" | awk 'BEGIN{FS=OFS="\t"}{a[$1 FS $2 FS $3]=a[$1 FS $2 FS $3]?a[$1FS$2FS$3] FS $4 FS $5:$4 FS $5}END{for(x in a) print x, a[x]}'
8
  • @user3086014 Make sure that you have copied the command exactly like I have given above. Commented Mar 26, 2014 at 5:06
  • yes i have copied the command exactly and should i write all this in a shell script? Commented Mar 26, 2014 at 5:14
  • @user3086014 Looks like you are running into an issue pasting multi-line command. I modified the answer to be all in one line. Try it out and see if that helps. No you dont need a shell script. You can run this on command line. Commented Mar 26, 2014 at 5:15
  • i have tried now there is another issue for some instances owner tag is oming first and for other name is coming first. Is this possible that we can use name, owner, instance id, cost centre as heading and below the, values and if a tag is blank is should be left blank or fill with NULL. Commented Mar 26, 2014 at 5:18
  • i have another command ec2-describe-instances |awk 'BEGIN{IGNORECASE=1}/(name|owner|cost.center)/&&/tag/' Can we write the values for i single instance in one line ? Commented Mar 26, 2014 at 7:09

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