y
y
yq
Search…
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

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.

Set line comment

Given a sample.yml file of:
1
a: cat
Copied!
then
1
yq eval '.a lineComment="single"' sample.yml
Copied!
will output
1
a: cat # single
Copied!

Use update assign to perform relative updates

Given a sample.yml file of:
1
a: cat
2
b: dog
Copied!
then
1
yq eval '.. lineComment |= .' sample.yml
Copied!
will output
1
a: cat # cat
2
b: dog # dog
Copied!

Set head comment

Given a sample.yml file of:
1
a: cat
Copied!
then
1
yq eval '. headComment="single"' sample.yml
Copied!
will output
1
# single
2
3
a: cat
Copied!

Set foot comment, using an expression

Given a sample.yml file of:
1
a: cat
Copied!
then
1
yq eval '. footComment=.a' sample.yml
Copied!
will output
1
a: cat
2
3
# cat
Copied!

Remove comment

Given a sample.yml file of:
1
a: cat # comment
2
b: dog # leave this
Copied!
then
1
yq eval '.a lineComment=""' sample.yml
Copied!
will output
1
a: cat
2
b: dog # leave this
Copied!

Remove (strip) all comments

Note the use of ... to ensure key nodes are included.
Given a sample.yml file of:
1
a: cat # comment
2
# great
3
b: # key comment
Copied!
then
1
yq eval '... comments=""' sample.yml
Copied!
will output
1
a: cat
2
b:
Copied!

Get line comment

Given a sample.yml file of:
1
a: cat # meow
Copied!
then
1
yq eval '.a | lineComment' sample.yml
Copied!
will output
1
meow
Copied!

Get head comment

Given a sample.yml file of:
1
a: cat # meow
Copied!
then
1
yq eval '. | headComment' sample.yml
Copied!
will output
1
Copied!

Get foot comment

Given a sample.yml file of:
1
a: cat # meow
Copied!
then
1
yq eval '. | footComment' sample.yml
Copied!
will output
1
Copied!