yq/pkg/yqlib/doc/Delete.md

118 lines
1.3 KiB
Markdown
Raw Normal View History

2020-11-17 22:44:16 +00:00
Deletes matching entries in maps or arrays.
2020-11-22 02:16:54 +00:00
## Delete entry in map
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval 'del(.b)' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
a: cat
2020-11-17 22:44:16 +00:00
```
2020-12-22 00:45:51 +00:00
## Delete nested entry in map
Given a sample.yml file of:
```yaml
a:
a1: fred
a2: frood
```
then
```bash
yq eval 'del(.a.a1)' sample.yml
```
will output
```yaml
a:
a2: frood
```
2020-11-22 02:16:54 +00:00
## Delete entry in array
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
- 1
- 2
- 3
```
then
```bash
yq eval 'del(.[1])' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
- 1
- 3
2020-11-17 22:44:16 +00:00
```
2020-12-22 00:45:51 +00:00
## Delete nested entry in array
Given a sample.yml file of:
```yaml
- a: cat
b: dog
```
then
```bash
yq eval 'del(.[0].a)' sample.yml
```
will output
```yaml
- b: dog
```
2020-11-22 02:16:54 +00:00
## Delete no matches
2020-11-17 22:44:16 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: dog
```
then
```bash
yq eval 'del(.c)' sample.yml
```
will output
```yaml
2020-11-19 05:45:05 +00:00
a: cat
b: dog
2020-11-17 22:44:16 +00:00
```
2020-11-22 02:16:54 +00:00
## Delete matching entries
2020-11-19 11:11:26 +00:00
Given a sample.yml file of:
```yaml
a: cat
b: dog
c: bat
```
then
```bash
yq eval 'del( .[] | select(. == "*at") )' sample.yml
```
will output
```yaml
b: dog
```
2021-01-08 00:59:49 +00:00
## Recursively delete matching keys
Given a sample.yml file of:
```yaml
a:
name: frog
b:
name: blog
age: 12
```
then
```bash
yq eval 'del(.. | select(has("name")).name)' sample.yml
```
will output
```yaml
a:
b:
age: 12
```