yq omit document dividers (---) in result
Asked Answered
R

2

6

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
Ratcliffe answered 6/10, 2021 at 21:50 Comment(0)
J
7

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
Jutta answered 7/10, 2021 at 13:50 Comment(0)
B
1

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
Belier answered 23/12, 2023 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.