How can I fetch few parameters in Shell script?

Hi All,

I want to fetch few details out of a huge output of AWS CLI tools :

I am using this command :

ec2-describe-instances

Replace the above awk with below

awk -F "\t" 'BEGIN{print "INSTANCE ID\tINSTANCE TYPE"} /INSTANCE/ {print $2 "\t" $7}'

---------- Post updated at 04:04 AM ---------- Previous update was at 04:01 AM ----------

ec2-describe-instances | awk -F "\t" 'BEGIN{print "INSTANCE ID\tINSTANCE TYPE"} /INSTANCE/ {print $2 "\t" $7}'

The output I am getting from your command is :

INSTANCE ID	INSTANCE TYPE
i-c		cloud
i-6		cloud
i-c		pub

Instead of picking up the instance type its picking up the keys

ec2-describe-instances | awk -F "\t" 'BEGIN{print "INSTANCE ID\tINSTANCE TYPE"} /INSTANCE/ {print $2 "\t" $9}'
1 Like

Thanks a bunch :slight_smile: can you help me further.

I have stored the output in a file called

Instances

The instances file has this detail:

INSTANCE ID         INSTANCE TYPE        INSTANCE STATE
i-8					t1.micro			  running
i-5					m3.medium			  running
i-b					m3.large			  running
i-e					m3.medium			  running
i-3					t1.micro			  running

I want to have a check that if the instance type is not t1.micro or t2.micro and the state is running then it should stop the instances with command

 ec2-stop-instances  instance id  

How can I implement this ?

awk -F "\t" 'NR > 1{if($2 != "t1.micro" && $2 != "t2.micro" && $3 == "running") print $1}' Instances | while read line
do
  ec2-stop-instances "${line}"
done

---------- Post updated at 06:28 AM ---------- Previous update was at 06:25 AM ----------

or

awk -F "\t" 'NR > 1{if($2 != "t1.micro" && $2 != "t2.micro" && $3 == "running") system("ec2-stop-instances " $1)}' Instances
1 Like

It worked.