Getting value from an array in bash

Have two variables with  ":" separated values
      e.g
      VAR1=one:two:three:four:five:six
      VAR2=ny:co:oh:nj:tx:fl

Request is:
Runtime, I will get any one value from VAR1 (e.g one or two or three...) in a loop
would like to get the corresponding value from VAR2 and concatnate again with ":"

if I get "one" and "five", then would like an ouput "ny:tx" in a variable
enthuguyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
#!/bin/bash
VAR1=one:two:three:four:five:six
VAR2=ny:co:oh:nj:tx:fl
ifs=$IFS
IFS=:
A1=($VAR1)
A2=($VAR2)
IFS=$ifs
for(( i=0; i<${#A1[*]}; ++i )) do eval ${A1[$i]}=${A2[$i]} ; done
for n in $* ; do variable+=${!n}: ; done;
variable=${variable%:}
echo $variable
Gerwin Jansen, EE MVETopic Advisor Commented:
How would you enter the loop and supply the values like "one" and "five"?
enthuguyAuthor Commented:
I can pass  "one" first and then "five"

Or

"one: five" in another variable too
VAR3="one: five"
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

ozoCommented:
#!/bin/bash
VAR1=one:two:three:four:five:six
VAR2=ny:co:oh:nj:tx:fl
VAR3="one: five"
ifs=$IFS
IFS=": "
A1=($VAR1)
A2=($VAR2)
A3=($VAR3)
IFS=$ifs
for(( i=0; i<${#A1[*]}; ++i )) do eval ${A1[$i]}=${A2[$i]} ; done
for n in ${A3[*]} ; do variable+=${!n}: ; done
variable=${variable%:}
echo $variable
enthuguyAuthor Commented:
HI ozo,
your solution really helped with the sample data i provided..thx

When I tried with different values...I'm getting error. could you help pls

e.g.
VAR1="one-one:two-two:three-3:four-4:five-5:six-6"
VAR2="ny.ny:co.co:oh.oh.oh:nj.nj.nj:tx.tx.tx:fl-fl"
VAR3="one-one:five-5"
ozoCommented:
If your version of bash supports declare -A
#!/bin/bash
VAR1="one-one:two-two:three-3:four-4:five-5:six-6"
VAR2="ny.ny:co.co:oh.oh.oh:nj.nj.nj:tx.tx.tx:fl-fl"
ifs=$IFS
IFS=:
A1=($VAR1)
A2=($VAR2)
A3=($VAR3)
IFS=$ifs
declare -A n
for(( i=0; i<${#A1[*]}; ++i )) do  n[${A1[$i]}]=${A2[$i]} ; done
for i in  ${A3[*]} ; do variable+=${n[$i]}: ; done;
variable=${variable%:}
echo $variable

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
enthuguyAuthor Commented:
Awesome, thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.