First, there are tools that are much better than
sed suited for manipulating the structured text data like YAML. E.g. see
this list.
Among others, there are two different, but similar tools, both named
yq, which are essentially the same to YAML what
jq is to JSON:
yq written in Python by Andrey Kislyuk, and
yq written in Go by Mike Farah.
This is how I would do it with the Python yq:
Code:
yq -Yi \
'del(.metadata)|
del(..|.uid?,.selfLink?,.resourceVersion?,.creationTimestamp?)' \
128-res-test-black-dev4-serviceaccounts.yaml
While the Python yq is just a wrapper around
jq, the Go yq is a rewrite that currently supports only a subset of jq commands. So for it, the expression above should be rewritten slightly more verbose:
Code:
yq -i eval \
'del(.metadata)|
del(.items[].metadata|(.uid,.selfLink,.resourceVersion,.creationTimestamp))' \
128-res-test-black-dev4-serviceaccounts.yaml
Quote:
Originally Posted by sysmicuser
Code:
sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /$ d' 128-res-test-black-dev4-serviceaccounts.yaml
|
You cannot combine regex with numeric addresses for one command in
sed (unless it's a range). Rewrite it using two
d commands:
Code:
sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /d;$d' 128-res-test-black-dev4-serviceaccounts.yaml
And using extended regular expressions this could be written more compactly:
Code:
sed -ri '/(uid|selfLink|resourceVersion|creationTimestamp): /d;$d' 128-res-test-black-dev4-serviceaccounts.yaml