yq/pkg/yqlib/doc/Comment Operators.md

136 lines
1.6 KiB
Markdown
Raw Normal View History

2020-11-17 22:44:16 +00:00
Use these comment operators to set or retrieve comments.
2020-11-22 02:16:54 +00:00
## Set line comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '.a lineComment="single"' sample.yml
```
will output
```yaml
a: cat # single
```
2021-01-06 09:22:50 +00:00
## Use update assign to perform relative updates
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval '.. lineComment |= .' sample.yml
```
will output
```yaml
a: cat # cat
b: dog # dog
```
2020-11-22 02:16:54 +00:00
## Set head comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '. headComment="single"' sample.yml
```
will output
```yaml
# single
a: cat
```
2020-11-22 02:16:54 +00:00
## Set foot comment, using an expression
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
```
then
```bash
yq eval '. footComment=.a' sample.yml
```
will output
```yaml
a: cat
# cat
```
2020-11-22 02:16:54 +00:00
## Remove comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # comment
b: dog # leave this
```
then
```bash
yq eval '.a lineComment=""' sample.yml
```
will output
```yaml
a: cat
b: dog # leave this
```
2020-11-22 02:16:54 +00:00
## Remove all comments
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # comment
```
then
```bash
yq eval '.. comments=""' sample.yml
```
will output
```yaml
a: cat
```
2020-11-22 02:16:54 +00:00
## Get line comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '.a | lineComment' sample.yml
```
will output
```yaml
meow
```
2020-11-22 02:16:54 +00:00
## Get head comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '. | headComment' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Get foot comment
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat # meow
```
then
```bash
yq eval '. | footComment' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
2020-11-17 22:44:16 +00:00
```