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

user3086014
  • 4,241
  • 5
  • 27
  • 56

1 Answers1

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]}'
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • @user3086014 Make sure that you have copied the command exactly like I have given above. – jaypal singh Mar 26 '14 at 05:06
  • yes i have copied the command exactly and should i write all this in a shell script? – user3086014 Mar 26 '14 at 05: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. – jaypal singh Mar 26 '14 at 05: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. – user3086014 Mar 26 '14 at 05: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 ? – user3086014 Mar 26 '14 at 07:09
  • please help me if you can. i am fed up and frustrated because of this. Please help me out. I will be very thankful to u – user3086014 Mar 27 '14 at 03:45
  • hey do you know how to parse json output in linux? – user3086014 Mar 27 '14 at 04:34
  • please have a look at my question :http://stackoverflow.com/questions/22678461/how-to-parse-json-with-shell-scripting-with-linux – user3086014 Mar 27 '14 at 04:45