Style
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style). Use this to control the formatting of the document in yaml.
Update and set style of a particular node (simple)
Given a sample.yml file of:
a:
b: thing
c: somethingthen
yq '.a.b = "new" | .a.b style="double"' sample.ymlwill output
a:
b: "new"
c: somethingUpdate and set style of a particular node using path variables
Given a sample.yml file of:
a:
b: thing
c: somethingthen
yq 'with(.a.b ; . = "new" | . style="double")' sample.ymlwill output
a:
b: "new"
c: somethingSet tagged style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="tagged"' sample.ymlwill output
!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
f: !!seq
- !!int 1
- !!int 2
- !!int 3
g: !!map
something: !!str coolSet double quote style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="double"' sample.ymlwill output
a: "cat"
b: "5"
c: "3.2"
e: "true"
f:
- "1"
- "2"
- "3"
g:
something: "cool"Set double quote style on map keys too
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '... style="double"' sample.ymlwill output
"a": "cat"
"b": "5"
"c": "3.2"
"e": "true"
"f":
- "1"
- "2"
- "3"
"g":
"something": "cool"Set single quote style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="single"' sample.ymlwill output
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
f:
- '1'
- '2'
- '3'
g:
something: 'cool'Set literal quote style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="literal"' sample.ymlwill output
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
f:
- |-
1
- |-
2
- |-
3
g:
something: |-
coolSet folded quote style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="folded"' sample.ymlwill output
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
f:
- >-
1
- >-
2
- >-
3
g:
something: >-
coolSet flow quote style
Given a sample.yml file of:
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolthen
yq '.. style="flow"' sample.ymlwill output
{a: cat, b: 5, c: 3.2, e: true, f: [1, 2, 3], g: {something: cool}}Reset style - or pretty print
Set empty (default) quote style, note the usage of ... to match keys too. Note that there is a --prettyPrint/-P short flag for this.
Given a sample.yml file of:
{a: cat, "b": 5, 'c': 3.2, "e": true, f: [1,2,3], "g": { something: "cool"} }then
yq '... style=""' sample.ymlwill output
a: cat
b: 5
c: 3.2
e: true
f:
- 1
- 2
- 3
g:
something: coolSet style relatively with assign-update
Given a sample.yml file of:
a: single
b: doublethen
yq '.[] style |= .' sample.ymlwill output
a: 'single'
b: "double"Read style
Given a sample.yml file of:
{a: "cat", b: 'thing'}then
yq '.. | style' sample.ymlwill output
flow
double
singleLast updated
Was this helpful?