Reputation: 195
I want to merge two yaml files using yq in bash script.Selecting only specific keys values , removing duplicates and building a string.
file1.yaml
FT1: true
FT2: true
file2.yaml
FT1: false
FT2: false
FT3: false
FT4: false
Blah1: abc
Blah2: efg
Desired Output:
Trying to build a command like below.
Filter any keys that starts with (FT*)
join the key=values
removing FT1, FT2 from file2.yaml
And building a command for each key=value pair appending it with --from-literal=
--from-literal=FT1=true --from-literal=FT2=true --from-literal=FT3=false --from-literal=FT4=false
What I have tried so far:
yq eval '. | to_entries| .[] | select(.key == "FT*") | [.key, .value] | join("=") file1.yaml
The above command output is as below. Not sure how to merge them , remove duplicates from file2 and build the command.
file1.yaml
FT1=true
FT2=true
file2.yaml
FT1=false
FT2=false
FT3=false
FT4=false
Upvotes: 0
Views: 641
Reputation: 36033
With mikefarah/yq, you can load
the other file, then merge it by adding:
yq '. + load("file1.yaml") | .[] | select(key == "FT*") | key + "=" + .' file2.yaml
FT1=true
FT2=true
FT3=false
FT4=false
You could also convert to_props
if it suits your use-case:
yq 'pick(keys | map(select(. == "FT*"))) + load("file1.yaml") | to_props' file2.yaml
FT1 = true
FT2 = true
FT3 = false
FT4 = false
Upvotes: 0