Galileo123
Galileo123

Reputation: 195

Yq: Merge two yaml file selecting keys using wildcard character and removing duplicates

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.

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

Answers (1)

pmf
pmf

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

Related Questions