I apologize for the title I do not know how to describe what I am trying to do so I will explain via code.
read a b <<<$(`ec2-describe-instances --region $REGION | awk '/SECURITY_ROLE/ { print $2 "\n" $5 }'`)
unfortunately this is not all I need to do.. from there I need to take the two arrays and overlay them into a single associative array.
I could potentially use a loop to do this. but the main issue is the command above.
Using the read a b <<<$(command) it doesn't set an array. and in the case the output is a 1 b 2 the end result would be as follows:
# echo ${a[0]}
a
# echo ${a[1]}
# echo ${b[0]}
1 b 2
# echo ${b[1]}
#
As you can see, if the content is greater than the number of variables, it doesn't loop back through the variables adding onto the array (although its not concerning itself with an array).
If I use the readarray command, I still do not receive the desired output.
My desired output is... ( where c is an associative array created from a and b )
# for i in `echo ${a[*]}` ; do echo ${c[$i]} ; done
1
2
In otherwords in the script I am creating I need to be able to call the $5 value from awk using the $2 value from awk without needing to run the ec2-describe-instances command more than once (it takes a long time).
EDIT
I couldn't get the ideal solution... I'm sure its out there somewhere, but I ended up going with a dirty work-around inspired by A.Danischewski after about 2 days of searching.
readarray HA_NODE_DIRECTORY < <(ec2-describe-instances --region $REGION | \
awk '/HA_Monitor/ { print $2, $5 }')
readarray HA_NODE_LIST < <(OIFS=$IFS
IFS=$'\n'
for i in ${HA_NODE_DIRECTORY[@]}
do
echo $i | awk '{ print $1 }'
done
IFS=$OIFS)
with another dirty function to tie them together instead of a clean Dictionary/Associative Array
function find_hostname {
ID=$1
OIFS=$IFS
IFS=$'\n'
for i in ${HA_NODE_DIRECTORY[@]}
do
echo $i | awk -v x=$ID '$0 ~ x { print $2 }'
done
IFS=$OIFS
}
ec2-describe-instances --region $REGION | awk '/SECURITY_ROLE/ { print $2 "\n" $5 }'in the question and desribe what are the two arrays you need. I feel this is xy problem.a 1 b 2(indicating two entries for$2and two entries for$5) then in the end, I am looking to somehow combine these four values into a associative arraya:1 b:2so to speak. Since in the outputawill always relate to1,bto2and so on... although I am stuck in general on splitting the outputs using a single command. I am confident that I can do it with two commands. The issue is that the AWS command is heavy, and I don't want to run it often.