In yq 4.11.2, If I do this:
echo '---\nfoo: foo\n---\nfoo: foo\n' | yq eval '.foo' -
I get this:
foo
---
foo
Using yq only (not grep, awk, etc.), how can I remove the ---
to get this?
foo
foo
In yq 4.11.2, If I do this:
echo '---\nfoo: foo\n---\nfoo: foo\n' | yq eval '.foo' -
I get this:
foo
---
foo
Using yq only (not grep, awk, etc.), how can I remove the ---
to get this?
foo
foo
There is a problem with your echo
invocation, that it does not expand literal newline characters by default, unless you run with echo -e
or enable xpg_echo
shell option if you are running this on bash/zsh
mikefarah/yq implementation has a mode -N
that prints the filter output with out the document separators See yq --help
when tested on version 4.13.3
echo -e '---\nfoo: foo\n---\nfoo: foo\n' | yq -N e '.foo' -
foo
foo
In powershell
short answer:
$yaml_content -replace "---",""
practical use case:
I wanted to identify duplicate objects in my cluster manifest
# foreach document collect kind, name, namespace into an array. Default namespace to null.
$select_fields=$cluster_manifest_yaml | yq '[ { "kind" : .kind , "name" : .metadata.name , "namespace" : .metadata.namespace // null } ]'
# remove doc separator. Reduce to single array.
$separator_removed=$select_fields -replace '---',''
# convert yaml array to pscustomobject array
$countable_array=$separator_removed | yq -o json | ConvertFrom-Json
# display duplicate objects in array
$countable_array | Group-Object kind,namespace,name | Select-Object Name,Count | Where-Object Count -gt 1
© 2022 - 2024 — McMap. All rights reserved.