Kevin
Kevin

Reputation: 2688

Bash Array From ec2 describe-images

How can I get an array like ARR[ImageID]=CreationDate from the tab-delimited output from my describe-images variable?

IMAGES=$(aws ec2 describe-images --output text --query 'Images[].[ImageId,CreationDate]');

Output is similar to:

ami-11xxxxx    2015-03-06:12:00:00
ami-12xxxxx    2015-03-06:12:00:00
ami-13xxxxx    2015-03-06:12:00:00

Upvotes: 1

Views: 232

Answers (2)

V. Michel
V. Michel

Reputation: 1619

#!/bin/bash 

#your data
IMAGES="
ami-11xxxxx    2015-03-06:12:00:11
ami-12xxxxx    2015-03-06:12:00:12
ami-13xxxxx    2015-03-06:12:00:13  
"

#declare associative memory
typeset -A ARR

index=""
for s in ${IMAGES}
do
   if [ -z ${index} ]; then
     index=$s
   else
     ARR[${index}]=$s
     index=""
   fi
done 

#test
echo ${ARR[ami-11xxxxx]}
echo ${ARR[ami-12xxxxx]}

result :

 2015-03-06:12:00:11
 2015-03-06:12:00:12

Upvotes: 1

BMW
BMW

Reputation: 45293

how about this?

IMAGES=$(aws ec2 describe-images --output text --query 'Images[].[ImageId,CreationDate]' |awk '{print $1 "=" $2}');

Upvotes: 1

Related Questions