Search code examples
yamlyq

Get all keys values using yq if a given string matches in YAML


I have below yaml file and I am trying to get all key value pairs if abc string is present in values.

a: abcsdf
b: hyuabc
c: iopabc
d: mnhbvgc

Expected output:

a: abcsdf
b: hyuabc
c: iopabc

My Attempt:

yq eval '.[] | select(. == "*abc*") test.yaml

Solution

  • It is really weird, the select and del functions in work totally different. While select returns the result of the filter as the array elements, delete returns the map

    yq eval 'del( .[] | select( . != "*abc*" ) )' yaml