Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i check if specific value exist in list with yq

Tags:

linux

bash

yq

How can i check if specific value exist in list with yq . This is my yaml

projects:
   p1:
     - "sample/project/app1"
     - "sample/project/app2"

This is my test code but it return false

cat test.yaml | yq '.projects.p1 | has("sample/project/app1")'
false
like image 993
rgd Avatar asked Oct 29 '25 01:10

rgd


2 Answers

Please specify which implementation of yq you are using.

With mikefarah/yq, you could use any_c:

yq '.projects.p1 | any_c(. == "sample/project/app1")' test.yaml

With kislyuk/yq, you could use IN:

yq 'IN(.projects.p1[]; "sample/project/app1")' test.yaml
like image 57
pmf Avatar answered Oct 30 '25 16:10

pmf


You cannot check whether array contains a string with has(). Please try instead:

yq < test.yaml '.projects.p1 | index("sample/project/app1") != null'
true
like image 28
tshiono Avatar answered Oct 30 '25 16:10

tshiono



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!