Skip to content

Instantly share code, notes, and snippets.

@rolroralra
Last active July 8, 2021 18:10
Show Gist options
  • Save rolroralra/4c57dc32814554e30fcd0be9cf3d40ca to your computer and use it in GitHub Desktop.
Save rolroralra/4c57dc32814554e30fcd0be9cf3d40ca to your computer and use it in GitHub Desktop.
yaml.md

merge key in yaml &, <<, * (anchor and inheritance in yaml)

Example

- &CENTER
  x: 1
  y: 2
- &LEFT
  x: 0
  y: 2
- &BIG
  r: 10
- &SMALL
  r: 1
- !!merge <<: *CENTER
  r: 10
$ yq eval '.[4]' sample.yaml
!!merge <<: *CENTER
r: 10

# Merge
$ yq eval '.[4] | explode(.)' sample.yaml 
x: 1
y: 2
r: 10
   
# Get Anchor
$ yq eval '.[0] | anchor' sample.yaml
CENTER
- &CENTER
  x: 1
  y: 2
- &LEFT
  x: 0
  y: 2
- &BIG
  r: 10
- &SMALL
  r: 1
- !!merge <<:
    - *CENTER
    - *BIG
$ yq eval '.[4]' sample.yaml
!!merge <<:
  - *CENTER
  - *BIG
  
$ yq eval '.[4] | explode(.)' sample.yaml
x: 1
y: 2
r: 10


new line folded(>) & new line literal(|)

https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-over-multiple-lines


yq

https://mikefarah.gitbook.io/yq/

How to install

export VERSION=v4.2.0
export BINARY=yq_linux_amd64
wget https://github.com/mikefarah/yq/releases/download/${VERSION}/${BINARY} -O /usr/bin/yq &&\
    chmod +x /usr/bin/yq


Alternative (Default Value)

yq eval --null-input '.a // "Default Value"'

Update node from another file

$ cat <<EOF > sample.yml
a: apples
EOF

$ cat <<EOF > another.yml
b: bob
EOF

$ yq eval-all 'select(fileIndex==0).a = select(fileIndex==1) | select(fileIndex==0)' sample.yml another.yml
a:
  b: bob

Update multiple paths

$ cat <<EOF > sample.yml
a: fieldA
b: fieldB
c: fieldC
EOF

$ yq eval '(.a, .c) |= "potatoe"' sample.yml
a: potatoe
b: fieldB
c: potatoe

Update Selected

$ cat <<EOF > sample.yml
a:
  b: apple
  c: cactus
EOF

$ yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
a:
  b: frog
  c: cactus
  
$ rm -f sample.yml && cat <<EOF > sample.yml
- candy
- apple
- sandy
EOF

$ yq eval '(.[] | select(. == "*andy")) = "bogs"' sample.yml
- bogs
- apple
- bogs

any_c, all_c

$ cat <<EOF > sample.yml
a:
  - rad
  - awesome
b:
  - meh
  - whatever
EOF

$ yq eval '.[] |= any_c(. == "awesome")' sample.yml
a: true
b: false

$ rm -f sample.yml && cat <<EOF > sample.yml
a:
  - rad
  - awesome
b:
  - meh
  - 12
EOF

$ yq eval '.[] |= all_c(tag == "!!str")' sample.yml
a: true
b: false

tag (node type)

  • !!map
  • !!seq
  • !!str
  • !!int

comments

$ cat <<EOF > sample.yml
a: cat
b: dog
c:
  d:
  e:
EOF

$ yq eval '.. lineComment |= .' sample.yml
a: cat # cat
b: dog # dog
c:
  d:
  e:

# Note the use of ... to ensure key nodes are included.
$ yq eval '... lineComment |= .' sample.yml
a: cat # cat
b: dog # dog
c: # c
  d: # d
  e: # e

remove comments

$ cat <<EOF > sample.yml
a: cat # comment
# great
b: # key comment
EOF

$ yq eval '... comments=""' sample.yml
a: cat
b:

delete keys in map

$ cat <<EOF > sample.yml
a:
  name: frog
  b:
    name: blog
    age: 12
EOF

$ yq eval 'del(.. | select(has("name")).name)' sample.yml
a:
  b:
    age: 12
    
$ yq eval '(.. | select(has("name")).name) = "test"' sample.yml
a:
  name: test
  b:
    name: test
    age: 12

Print Document Index with matches

$ cat <<EOF > sample.yml
a: cat
---
a: frog
EOF

$ yq eval '.a | ({"match": ., "doc": documentIndex})' sample.yml
match: cat
doc: 0
match: frog
doc: 1

Merge Files

yq eval-all 'select(fi == 0) * select(filename == "file2.yaml")' file1.yaml file2.yaml

has

$ cat <<EOF > sample.yml
- a:
    b:
      c: cat
- a:
    b:
      d: dog
EOF

$ yq eval '.[] | select(.a.b | has("c"))' sample.yml
a:
  b:
    c: cat

multiply (Merge)

https://mikefarah.gitbook.io/yq/v/v4.x/operators/multiply-merge

$ cat <<EOF > sample.yml
- a:
    b:
      c: cat
- a:
    b:
      d: dog
EOF

# Merge, appending arrays
$ yq eval '.[] | select(.a.b | has("c"))' sample.yml
a:
  b:
    c: cat
$ rm -f sample.yml && cat <<EOF > sample.yml
a:
  thing:
    - 1
    - 2
b:
  thing:
    - 3
    - 4
  another:
    - 1
EOF

# Merge, only existing fields, appending arrays
$ yq eval '.a *?+ .b' sample.yml
thing:
  - 1
  - 2
  - 3
  - 4
$ rm -f sample.yml && cat <<EOF > sample.yml
a:
  - name: fred
    age: 12
  - name: bob
    age: 32
b:
  - name: fred
    age: 34
EOF

# Merge, deeply merging arrays  
$ yq eval '.a *d .b' sample.yml
- name: fred
  age: 34
- name: bob
  age: 32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment