user784637
user784637

Reputation: 16112

How to use awk to parse values from stdout as elements of an array?

The command elb-describe-instance-health returns the following

INSTANCE_ID  i-111
INSTANCE_ID  i-222
INSTANCE_ID  i-333

$(elb-describe-instance-health | awk '/INSTANCE_ID/{print $2}')

returns i-111 i-222 i-3333

How can I change the above syntax to store each of these values in an array (ex. foo[0] equals i-111, foo[1] equals i-222, foo[2] equals i-333?

Upvotes: 2

Views: 4484

Answers (2)

Steve
Steve

Reputation: 54392

Here's one way:

array=($(elb-describe-instance-health | awk '/INSTANCE_ID/ { print $2 }'))

Then simply echo the element you want. To echo the first element for example, try:

echo "${array[0]}"

Upvotes: 7

Diego Torres Milano
Diego Torres Milano

Reputation: 69198

Use

$(elb-describe-instance-health |awk '/INSTANCE_ID/ { foo[i++] = $2 }')

but I guess you would like to do something with foo.

Upvotes: 2

Related Questions