'Get keys from yaml file using bash [duplicate]
Let's say I have a yaml file like this one:
env:
firstVar: true
secondVar: 20
aa_thirdVar: 'hello'
aa_fourthVar: false
and I need to get the keys under the env key except for the keys with aa_ prefix as a bash array in order to use the array in a bash script. I need to do this using standard Linux tools like sed, awk etc. without external dependencies. How can accomplish that?
Solution 1:[1]
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" )
Solution 2:[2]
i use yq that is wrapper for jq tool https://github.com/kislyuk/yq
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dawg |
| Solution 2 | David |
