2021-10-30 03:14:39 +00:00
|
|
|
# Tag
|
|
|
|
|
|
|
|
The tag operator can be used to get or set the tag of nodes (e.g. `!!str`, `!!int`, `!!bool`).
|
|
|
|
|
2022-02-06 03:41:27 +00:00
|
|
|
{% hint style="warning" %}
|
|
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
|
|
|
|
`yq e <exp> <file>`
|
|
|
|
{% endhint %}
|
|
|
|
|
2021-10-30 03:14:39 +00:00
|
|
|
## Get tag
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
f: []
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.. | tag' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
!!map
|
|
|
|
!!str
|
|
|
|
!!int
|
|
|
|
!!float
|
|
|
|
!!bool
|
|
|
|
!!seq
|
|
|
|
```
|
|
|
|
|
|
|
|
## Set custom tag
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: str
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '.a tag = "!!mikefarah"' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: !!mikefarah str
|
|
|
|
```
|
|
|
|
|
|
|
|
## Find numbers and convert them to strings
|
|
|
|
Given a sample.yml file of:
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: 5
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
|
|
|
then
|
|
|
|
```bash
|
2022-01-28 01:45:43 +00:00
|
|
|
yq '(.. | select(tag == "!!int")) tag= "!!str"' sample.yml
|
2021-10-30 03:14:39 +00:00
|
|
|
```
|
|
|
|
will output
|
|
|
|
```yaml
|
|
|
|
a: cat
|
|
|
|
b: "5"
|
|
|
|
c: 3.2
|
|
|
|
e: true
|
|
|
|
```
|
2021-11-03 04:00:28 +00:00
|
|
|
|