The first thing is produce key=value
pairs.
You can use ruby
with the built in yaml parser:
ruby -r yaml -e 'data=YAML.load($<.read)
data["env"].
select{|k,v| k.to_s.match(/^(?!aa)/)}.
each{|k,v| puts "#{k}=#{v}"}
' file
Or, more fragile, you could use this awk
:
awk '
/^env:/{con=$1; next}
$1~/aa_/{next}
con=="env"
{sub(/:$/,"",$1); print $1 "=" $2}' file
Either of those prints:
firstVar=true
secondVar=20
Now have a Bash loop that will add key=value type pairs to an associative array aa
like so:
declare -A aa
while IFS="=" read -r k v; do
echo "$k $v"
aa["$k"]="$v"
done < <(ruby -r yaml -e 'data=YAML.load($<.read)
data["env"].
select{|k,v| k.to_s.match(/^(?!aa)/)}.
each{|k,v| puts "#{k}=#{v}"}
' file)
Result:
declare -p aa
Prints:
declare -A aa=([firstVar]="true" [secondVar]="20" )