Skip to content

Instantly share code, notes, and snippets.

@InfoSec812
Last active June 11, 2024 11:22
Show Gist options
  • Save InfoSec812/0d5a3d5322d4edf4466a663537bf2cc0 to your computer and use it in GitHub Desktop.
Save InfoSec812/0d5a3d5322d4edf4466a663537bf2cc0 to your computer and use it in GitHub Desktop.
Find all keys which have a value matching a string, then output the full "path" to those keys

In Windows Powershell, you need some extra "escapes"

yq '.. | ( select (.[] == \"*<target>*\" and keys and parent)) | (path | join(\".\"))' <input file>.yaml \ | sed -E "s@\\.\([0-9]+\)\(\.|$\)@[\\1]\\2@g"

On *NIX, you can use the cleaner form:

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment