0

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
}
2
  • Post the output of 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. Commented Jun 3, 2016 at 3:35
  • This very well may be an xy problem, although I did give an example of x at the end. If the provided command outputs a 1 b 2 (indicating two entries for $2 and two entries for $5) then in the end, I am looking to somehow combine these four values into a associative array a:1 b:2 so to speak. Since in the output a will always relate to 1, b to 2 and 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. Commented Jun 3, 2016 at 4:25

1 Answer 1

1

I'm not sure what ec2-describe-instances --region $REGION | awk '/SECURITY_ROLE/ { print $2 "\n" $5 }' outputs, but it looks like you want the results in an array.

You can set an array with read -a:
read -d'\n' -a a < <(printf "Field1 Field2_%s\n" {1..10})

$ echo ${a[2]}
Field1

$ echo ${a[3]}
Field2_2

For two arrays:

unset a b && tmpfile="/tmp/_arr2tmp_$$_" 
printf "Field1 Field2_%s\n" {1..10} > "${tmpfile}"
read -d'\n' -a a < <(awk '{print $1}' "${tmpfile}")
read -d'\n' -a b < <(awk '{print $2}' "${tmpfile}")
echo "First three values of a: ${a[1]},${a[2]},${a[3]}"
echo "First three values of b: ${b[1]},${b[2]},${b[3]}"
rm "${tmpfile}"
Sign up to request clarification or add additional context in comments.

5 Comments

I want the results in two separate arrays one to hold the contents of $2 and one to hold the contents of $5 which from the aws command would be instance-id and hostname. Ill try to see if I can get it to work with -a and let you know how it goes.
I tried this read -d "\n" -a a -a b <<<$(ec2-describe-instances --region $REGION | awk '/HA_Monitor/ { print $2 } /HA_Monitor/ { print $5 }') and it kind of worked except my output was all placed into the b array... I wonder if there is a way to get it to utilize multiple variables alternating inputs.
If you use process substitution you can put them into an array and every other element will be the output, yet you have to be careful about any missed value fields.
I tried it like this read -d'\n' -a a -a b < <(printf "Field1 Field2_%s\n" {1..10}) but for the two variables defined for read, a does not recieve any of the fields, while b recieves all of the fields echo ${b[19]} => Field2_10. What I would like to see is the Field1 values all be a part of ${a[*]} and all of the Field2_%s` values to be part of the ${b[*]} field if possible.
For two arrays if the input is not too big, you can first dump the output to a file and then read the columns you want into arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.