mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-15 07:38:14 +00:00
227 lines
3.3 KiB
Markdown
227 lines
3.3 KiB
Markdown
# Comment Operators
|
|
|
|
Use these comment operators to set or retrieve comments. Note that line comments on maps/arrays are actually set on the _key_ node as opposed to the _value_ (map/array). See below for examples.
|
|
|
|
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
|
|
|
|
### relative form: `|=`
|
|
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.
|
|
|
|
{% hint style="warning" %}
|
|
Note that versions prior to 4.18 require the 'eval/e' command to be specified. 
|
|
|
|
`yq e <exp> <file>`
|
|
{% endhint %}
|
|
|
|
## Set line comment
|
|
Set the comment on the key node for more reliability (see below).
|
|
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat
|
|
```
|
|
then
|
|
```bash
|
|
yq '.a line_comment="single"' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a: cat # single
|
|
```
|
|
|
|
## Set line comment of a maps/arrays
|
|
For maps and arrays, you need to set the line comment on the _key_ node. This will also work for scalars.
|
|
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a:
|
|
b: things
|
|
```
|
|
then
|
|
```bash
|
|
yq '(.a | key) line_comment="single"' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a: # single
|
|
b: things
|
|
```
|
|
|
|
## Use update assign to perform relative updates
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat
|
|
b: dog
|
|
```
|
|
then
|
|
```bash
|
|
yq '.. line_comment |= .' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a: cat # cat
|
|
b: dog # dog
|
|
```
|
|
|
|
## Set head comment
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat
|
|
```
|
|
then
|
|
```bash
|
|
yq '. head_comment="single"' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
# single
|
|
|
|
a: cat
|
|
```
|
|
|
|
## Set head comment of a map entry
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
f: foo
|
|
a:
|
|
b: cat
|
|
```
|
|
then
|
|
```bash
|
|
yq '(.a | key) head_comment="single"' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
f: foo
|
|
# single
|
|
a:
|
|
b: cat
|
|
```
|
|
|
|
## Set foot comment, using an expression
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat
|
|
```
|
|
then
|
|
```bash
|
|
yq '. foot_comment=.a' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a: cat
|
|
# cat
|
|
```
|
|
|
|
## Remove comment
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat # comment
|
|
b: dog # leave this
|
|
```
|
|
then
|
|
```bash
|
|
yq '.a line_comment=""' sample.yml
|
|
```
|
|
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
|
|
yq '... comments=""' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
a: cat
|
|
b:
|
|
```
|
|
|
|
## Get line comment
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
a: cat # meow
|
|
# have a great day
|
|
```
|
|
then
|
|
```bash
|
|
yq '.a | line_comment' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
meow
|
|
```
|
|
|
|
## Get head comment
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
# welcome!
|
|
|
|
a: cat # meow
|
|
|
|
# have a great day
|
|
```
|
|
then
|
|
```bash
|
|
yq '. | head_comment' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
welcome!
|
|
```
|
|
|
|
## Head comment with document split
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
# welcome!
|
|
---
|
|
# bob
|
|
a: cat # meow
|
|
|
|
# have a great day
|
|
```
|
|
then
|
|
```bash
|
|
yq 'head_comment' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
welcome!
|
|
bob
|
|
```
|
|
|
|
## Get foot comment
|
|
Given a sample.yml file of:
|
|
```yaml
|
|
# welcome!
|
|
|
|
a: cat # meow
|
|
|
|
# have a great day
|
|
# no really
|
|
```
|
|
then
|
|
```bash
|
|
yq '. | foot_comment' sample.yml
|
|
```
|
|
will output
|
|
```yaml
|
|
have a great day
|
|
no really
|
|
```
|
|
|