yq/pkg/yqlib/doc/Style Operator.md

213 lines
2.2 KiB
Markdown
Raw Normal View History

2020-11-18 09:42:37 +00:00
The style operator can be used to get or set the style of nodes (e.g. string style, yaml style)
2020-11-17 22:44:16 +00:00
## Examples
2020-11-18 09:42:37 +00:00
### Set tagged style
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
2020-11-18 09:42:37 +00:00
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
then
```bash
2020-11-18 09:42:37 +00:00
yq eval '.. style="tagged"' sample.yml
2020-11-17 22:44:16 +00:00
```
will output
```yaml
2020-11-18 09:42:37 +00:00
!!map
a: !!str cat
b: !!int 5
c: !!float 3.2
e: !!bool true
```
### Set double quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="double"' sample.yml
```
will output
```yaml
a: "cat"
b: "5"
c: "3.2"
e: "true"
```
### Set single quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="single"' sample.yml
```
will output
```yaml
a: 'cat'
b: '5'
c: '3.2'
e: 'true'
```
### Set literal quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="literal"' sample.yml
```
will output
```yaml
a: |-
cat
b: |-
5
c: |-
3.2
e: |-
true
```
### Set folded quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="folded"' sample.yml
```
will output
```yaml
a: >-
cat
b: >-
5
c: >-
3.2
e: >-
true
```
### Set flow quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style="flow"' sample.yml
```
will output
```yaml
{a: cat, b: 5, c: 3.2, e: true}
```
### Set empty (default) quote style
Given a sample.yml file of:
```yaml
a: cat
b: 5
c: 3.2
e: true
```
then
```bash
yq eval '.. style=""' sample.yml
```
will output
```yaml
a: cat
b: 5
c: 3.2
e: true
2020-11-17 22:44:16 +00:00
```
### Set style using a path
Given a sample.yml file of:
```yaml
a: cat
b: double
```
then
```bash
yq eval '.a style=.b' sample.yml
```
will output
```yaml
2020-11-18 09:42:37 +00:00
a: "cat"
b: double
2020-11-17 22:44:16 +00:00
```
2020-11-18 09:42:37 +00:00
### Example 8
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval '.. style=""' sample.yml
```
will output
```yaml
a: cat
b: dog
```
2020-11-18 09:42:37 +00:00
### Example 9
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: thing
```
then
```bash
yq eval '.. | style' sample.yml
```
will output
```yaml
2020-11-18 09:42:37 +00:00
2020-11-17 22:44:16 +00:00
```
2020-11-18 09:42:37 +00:00
### Example 10
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '.. | style' sample.yml
```
will output
```yaml
```