Issue
I have this below YAML input and I am trying to extract shown output using yq
Input:
VAR-A: '{{a.b.VAR-A}}'
VAR-B: '{{a.b.VAR-B}}'
VAR-C: v0.0
VAR-D: '{{a.b.VAR-D}}-{{a.b.VAR-A}}'
VAR-E: '{{a.b.VAR-C}}-{{a.b.VAR-B}}-{{a.b.VAR-A}}'
VAR-F: '{{a.b.VAR-F}}'
Expected Output:
VAR-C: v0.0
VAR-D: '{{a.b.VAR-D}}-{{a.b.VAR-A}}'
VAR-E: '{{a.b.VAR-C}}-{{a.b.VAR-B}}-{{a.b.VAR-A}}'
My Attempt:
yq eval 'del( .[] | select (. == "{{a.b.*" ) )' abc.yaml
I am new to yq
and any help will be much appreciated.
Based on @Inian's comment
**Note: I want to remove pairs where key name (VAR-A) in value {{a.b.VAR-A}} (after a.b.) matches and If I have more than {{a.b.VAR-A}} in values separated by - , I want to keep them
Solution
Is this what you expected ?
yq eval 'del(.[] | select(sub("^{{a\.b\.[^}]+}}$","")=="" ))' abc.yaml
The regex ^{{a\.b\.[^}]+}}$
matches a single {{...}} structure, because of ^
and $
at the beginning and end. [^}]
means any character which is not }
.
Then it substitute with empty string, if the result is again an empty string, it's selected for deletion.
If you just want to match {{a.b.VAR-A}}
:
yq eval 'del(.[] | select(sub("^{{a\.b\.VAR-A}}$","")=="" ))' abc.yaml
Answered By - Philippe