title: Controlled Iteration through bash array using nested for loop
body: <p>common.yml file</p>
  <pre><code>tomcat9_version:               &quot;9.0.64-154&quot;
  openjdk11_version:                &quot;11.54.24-95&quot;
  newrelic_infra_agent_version:  &quot;0.1.22-42&quot;
  </code></pre>
  <p>vars.yml</p>
  <pre><code>Tomcat:
    packages:
      OpenJDK11:
        - 11.52.24
      Tomcat9:
        - 9.0.50
     newrelic_infra:
        - 0.1.21
  </code></pre>
  <p>Capture values of tomcat9_version, openjdk11_version, newrelic_infra_agent_version from common.yml and replace OpenJDK11, Tomcat9, newrelic_infra values in vars.yml</p>
  <p>What i wrote</p>
  <pre><code>#!/bin/bash
  declare -a arr=(&quot;openjdk11_version&quot; &quot;tomcat9_version&quot; &quot;newrelic_infra_agent_version&quot;)
  declare -a brr=(&quot;OpenJDK11:&quot; &quot;Tomcat9:&quot; &quot;newrelic_infra:&quot;)
  for i in &quot;${arr[@]}&quot;
  do
     value=$(grep &quot;$i&quot; common.yaml  | awk '{print $NF}')
     echo &quot;$value&quot;
      for i in &quot;${brr[@]}&quot;
      do
        pakru=$(awk -v var=$i '$1 == &quot;-&quot;{ if (key == var) print $NF;next } {key=$1}' vars.yaml)
        echo &quot;$pakru&quot;
        sed -i &quot;s/$pakru/$value/g&quot; &quot;vars.yaml&quot;
        break
     done
  done
  </code></pre>
  <p>The output i got</p>
  <pre><code>&quot;11.54.24-95&quot;
  11.52.24
  9.0.50
  0.1.21
  &quot;9.0.64-154&quot;
  11.52.24
  9.0.50
  0.1.21
  &quot;0.1.22-42&quot;
  11.52.24
  9.0.50
  0.1.21
  </code></pre>
  <p>expected output:-</p>
  <pre><code>&quot;11.54.24-95&quot;
  11.52.24
  &quot;9.0.64-154&quot;
  9.0.50
  &quot;0.1.22-42&quot;
  0.1.21
  </code></pre>