Skip to content

Instantly share code, notes, and snippets.

@dragonde
Last active April 25, 2024 19:27
Show Gist options
  • Save dragonde/055677fba24d2be9eb037e0367febb30 to your computer and use it in GitHub Desktop.
Save dragonde/055677fba24d2be9eb037e0367febb30 to your computer and use it in GitHub Desktop.
yq cheat sheet

curl http://plytix-charts.core.private-plytix.com:8080/api/charts -o charts.json

cat charts.json | yq e -P ' .[].[] | select(.name =="mongo*") | (.name + "," + .version)'

merges f2.yml into f1.yml (inplace)

yq eval-all --inplace 'select(fileIndex == 0) * select(fileIndex == 1)' f1.yml f2.yml

you can merge into a file, piping from STDIN

cat somefile.yml | yq eval-all --inplace 'select(fileIndex == 0) * select(fileIndex == 1)' f1.yml -

yq eval-all 'select(fileIndex == 0) * select(fileIndex == 1)' a.yml b.yml

diff <(yq -P 'sort_keys(..)' -o=props a.yml) <(yq -P 'sort_keys(..)' -o=props b.yml)

#!/bin/bash

yq e '.adminer' B.yml > B0.yml yq e '.adminer' C.yml > C0.yml

yq ea 'select(fileIndex == 0) * select(fileIndex == 1)' A.yml B0.yml |
perl -pe 's|\s*#.+$||' |
perl -pe 's|^.+: []$||' | perl -pe 's|^.+: {}$||' |
perl -pe 's|^\s+$||' > B1.yml

yq ea 'select(fileIndex == 0) * select(fileIndex == 1)' A.yml C0.yml |
perl -pe 's|\s*#.+$||' |
perl -pe 's|^.+: []$||' | perl -pe 's|^.+: {}$||' |
perl -pe 's|^\s+$||' > C1.yml

yq ea 'select(fileIndex == 0) * select(fileIndex == 1)' B1.yml C0.yml |
perl -pe 's|\s*#.+$||' |
perl -pe 's|^.+: []$||' | perl -pe 's|^.+: {}$||' |
perl -pe 's|^\s+$||' > C2.yml

diff <(yq -P 'sort_keys(..)' -o=props C2.yml) <(yq -P 'sort_keys(..)' -o=props C1.yml) > diff.txt

cat diff.txt | grep -P '^>' | perl -pe 's|^> ||' > in.txt cat diff.txt | grep -P '^<' | perl -pe 's|^< (.+) = .+$|\1|' > out.txt

yq -oy -p=props -P in.txt > result.yml

yq ea 'select(fileIndex == 0) * select(fileIndex == 1)' C0.yml result.yml |
perl -pe 's|\s*#.+$||' |
perl -pe 's|^.+: []$||' | perl -pe 's|^.+: {}$||' |
perl -pe 's|^\s+$||' > final.yml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment