return first match of yaml containing multiple documents #1326
Replies: 4 comments 4 replies
-
I forced it to work doing this:
which returns: But was hoping for a more elegant way... |
Beta Was this translation helpful? Give feedback.
-
Just pipe the result through a select: curl -s https://raw.githubusercontent.com/sassoftware/viya4-deployment/main/roles/vdm/templates/resources/openldap.yaml | yq -N '.spec.template.spec.containers[0].image | select(. != null)' |
Beta Was this translation helpful? Give feedback.
-
I have two non-null results from two docs, but I only want the first (the second is an overlapping data set but I want to prioritise results from the first when they are available). Is there a simple way to only return the first result? |
Beta Was this translation helpful? Give feedback.
-
I solved this by converting the results to JSON then capturing in a bash array and just returning the first result. But then I came up with a different solution which I think is more elegant: The first yq command will return a separate document for each file which matches, and then the second yq command will return only the result from the first document. If only one file matches, then it will always be the first document, so it will always be used for the result. |
Beta Was this translation helpful? Give feedback.
-
Hi I have a simple yaml file, which contains multiple documents: https://raw.githubusercontent.com/sassoftware/viya4-deployment/main/roles/vdm/templates/resources/openldap.yaml
I'm simply trying to query the container image from the yaml.. and I first attempted this:
curl -s https://raw.githubusercontent.com/sassoftware/viya4-deployment/main/roles/vdm/templates/resources/openldap.yaml | yq -N '.spec.template.spec.containers[0].image'
which returns:
osixia/openldap:1.3.0
null
I realize it is returning null, because it is failing to find my query in the second document within this file. Is there an easy way with yq to only return the string I'm after, without null?
Beta Was this translation helpful? Give feedback.
All reactions