yq '.. | ( select (.[] == \"*<target>*\" and keys and parent)) | (path | join(\".\"))' <input file>.yaml \ | sed -E "s@\\.\([0-9]+\)\(\.|$\)@[\\1]\\2@g"
yq '.. | ( select (.[] == "*<target>*" and keys and parent)) | (path | join("."))' <input file>.yaml | sed -E "s@\.([0-9]+)(.|$)@[\1]\2@g"
Output:
objects[0].metadata
objects[1].metadata
objects[2].metadata
objects[2].spec.selector
objects[2].spec.ports[0]
objects[3].metadata
objects[3].stringData
objects[4].metadata
objects[4].metadata.labels
objects[4].spec
objects[4].spec.dnsNames
objects[4].spec.keystores.pkcs12.passwordSecretRef
objects[5].metadata
objects[5].metadata.labels
objects[5].spec.dataVolumeTemplates[0].metadata
objects[5].spec.template.metadata.labels
objects[5].spec.template.spec.domain.cpu
objects[5].spec.template.spec.domain.devices.interfaces[0]
objects[5].spec.template.spec.domain.memory
objects[5].spec.template.spec.networks[0].pod
objects[5].spec.template.spec.volumes[0].dataVolume
objects[5].spec.template.spec.volumes[1].configMap