yq/operators/comment-operators.md

188 lines
2.6 KiB
Markdown
Raw Normal View History

2021-10-30 03:14:39 +00:00
# Comment Operators
Use these comment operators to set or retrieve comments.
Like the `=` and `|=` assign operators, the same syntax applies when updating comments:
### plain form: `=`
This will assign the LHS nodes comments to the expression on the RHS. The RHS is run against the matching nodes in the pipeline
2021-11-03 04:00:28 +00:00
### relative form: `|=`
2021-10-30 03:14:39 +00:00
Similar to the plain form, however the RHS evaluates against each matching LHS node! This is useful if you want to set the comments as a relative expression of the node, for instance its value or path.
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
## Set line comment
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a lineComment="single"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: cat # single
```
## Use update assign to perform relative updates
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.. lineComment |= .' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: cat # cat
b: dog # dog
```
## Set head comment
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. headComment="single"' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
# single
a: cat
```
## Set foot comment, using an expression
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '. footComment=.a' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: cat
# cat
```
## Remove comment
Given a sample.yml file of:
```yaml
a: cat # comment
b: dog # leave this
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a lineComment=""' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: cat
b: dog # leave this
```
## Remove (strip) all comments
Note the use of `...` to ensure key nodes are included.
Given a sample.yml file of:
```yaml
a: cat # comment
# great
b: # key comment
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '... comments=""' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
a: cat
b:
```
## Get line comment
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
2022-01-28 01:45:43 +00:00
yq '.a | lineComment' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
meow
```
## Get head comment
Given a sample.yml file of:
```yaml
2021-11-03 04:00:28 +00:00
# welcome!
2021-10-30 03:14:39 +00:00
a: cat # meow
2021-11-03 04:00:28 +00:00
# have a great day
```
2021-10-30 03:14:39 +00:00
then
```bash
2022-01-28 01:45:43 +00:00
yq '. | headComment' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2021-11-03 04:00:28 +00:00
welcome!
2021-10-30 03:14:39 +00:00
```
2021-11-24 00:15:42 +00:00
## Head comment with document split
2021-11-14 00:10:43 +00:00
Given a sample.yml file of:
```yaml
# welcome!
---
# bob
a: cat # meow
# have a great day
```
then
```bash
2022-01-28 01:45:43 +00:00
yq 'headComment' sample.yml
2021-11-14 00:10:43 +00:00
```
will output
```yaml
welcome!
bob
```
2021-10-30 03:14:39 +00:00
## Get foot comment
Given a sample.yml file of:
```yaml
2021-11-03 04:00:28 +00:00
# welcome!
2021-10-30 03:14:39 +00:00
a: cat # meow
2021-11-03 04:00:28 +00:00
# have a great day
2021-11-14 00:10:43 +00:00
# no really
2021-11-03 04:00:28 +00:00
```
2021-10-30 03:14:39 +00:00
then
```bash
2022-01-28 01:45:43 +00:00
yq '. | footComment' sample.yml
2021-10-30 03:14:39 +00:00
```
will output
```yaml
2021-11-03 04:00:28 +00:00
have a great day
2021-11-14 00:10:43 +00:00
no really
2021-10-30 03:14:39 +00:00
```
2021-11-03 04:00:28 +00:00