diff --git a/pkg/yqlib/doc/Delete.md b/pkg/yqlib/doc/Delete.md index 4f214f79..8842b091 100644 --- a/pkg/yqlib/doc/Delete.md +++ b/pkg/yqlib/doc/Delete.md @@ -95,3 +95,23 @@ will output b: dog ``` +## 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 +``` + diff --git a/pkg/yqlib/doc/Recursive Descent.md b/pkg/yqlib/doc/Recursive Descent.md index ec3c585e..865306ec 100644 --- a/pkg/yqlib/doc/Recursive Descent.md +++ b/pkg/yqlib/doc/Recursive Descent.md @@ -1,4 +1,4 @@ -This operator recursively matches all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches. It can be used in either the +This operator recursively matches (or globs) all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches. It can be used in either the ## match values form `..` This will, like the `jq` equivalent, recursively match all _value_ nodes. Use it to find/manipulate particular values. diff --git a/pkg/yqlib/doc/headers/Recursive Descent.md b/pkg/yqlib/doc/headers/Recursive Descent.md index 7f688729..10bb6096 100644 --- a/pkg/yqlib/doc/headers/Recursive Descent.md +++ b/pkg/yqlib/doc/headers/Recursive Descent.md @@ -1,4 +1,4 @@ -This operator recursively matches all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches. It can be used in either the +This operator recursively matches (or globs) all children nodes given of a particular element, including that node itself. This is most often used to apply a filter recursively against all matches. It can be used in either the ## match values form `..` This will, like the `jq` equivalent, recursively match all _value_ nodes. Use it to find/manipulate particular values. diff --git a/pkg/yqlib/operator_delete_test.go b/pkg/yqlib/operator_delete_test.go index 54b4c020..f7fe831c 100644 --- a/pkg/yqlib/operator_delete_test.go +++ b/pkg/yqlib/operator_delete_test.go @@ -61,6 +61,14 @@ var deleteOperatorScenarios = []expressionScenario{ "D0, P[], (doc)::{b: dog}\n", }, }, + { + description: "Recursively delete matching keys", + document: `{a: {name: frog, b: {name: blog, age: 12}}}`, + expression: `del(.. | select(has("name")).name)`, + expected: []string{ + "D0, P[], (!!map)::{a: {b: {age: 12}}}\n", + }, + }, } func TestDeleteOperatorScenarios(t *testing.T) {