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.
Note that versions prior to 4.18 require the 'eval/e' command to be specified.
yq e <exp> <file>

Set line comment

Given a sample.yml file of:
1
a: cat
Copied!
then
1
yq '.a line_comment="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 '.. line_comment |= .' 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 '. head_comment="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 '. foot_comment=.a' sample.yml
Copied!
will output
1
a: cat
2
# cat
Copied!

Remove comment

Given a sample.yml file of:
1
a: cat # comment
2
b: dog # leave this
Copied!
then
1
yq '.a line_comment=""' 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 '... 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
2
# have a great day
Copied!
then
1
yq '.a | line_comment' sample.yml
Copied!
will output
1
meow
Copied!

Get head comment

Given a sample.yml file of:
1
# welcome!
2
3
a: cat # meow
4
5
# have a great day
Copied!
then
1
yq '. | head_comment' sample.yml
Copied!
will output
1
welcome!
Copied!

Head comment with document split

Given a sample.yml file of:
1
# welcome!
2
---
3
# bob
4
a: cat # meow
5
6
# have a great day
Copied!
then
1
yq 'head_comment' sample.yml
Copied!
will output
1
welcome!
2
bob
Copied!

Get foot comment

Given a sample.yml file of:
1
# welcome!
2
3
a: cat # meow
4
5
# have a great day
6
# no really
Copied!
then
1
yq '. | foot_comment' sample.yml
Copied!
will output
1
have a great day
2
no really
Copied!