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?
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
0
Gerwin Jansen, EE MVETopic Advisor Commented:
How would you enter the loop and supply the values like "one" and "five"?
0
enthuguyAuthor Commented:
I can pass  "one" first and then "five"

Or

"one: five" in another variable too
VAR3="one: five"
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
1
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"
0
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
0

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.