<p>I have a file containing many Kubernetes YAML objects.
I am seeking a way of removing all K8s Secret YAML objects from the text file, identified by the <code>&quot;kind: Secret&quot;</code> string contained within the YAML block. This should remove everything from the &quot;apiVersion&quot; through to just before the &quot;---&quot; signifying the start of the next object.</p>
<p>I've looked into Sed, Python and yq tools with no luck.
The YAML may contain any number of secrets in any order.</p>
<p>How can I automate stripping out of these &quot;Secret&quot; blocks?</p>
<pre><code>apiVersion: v1
data:
  username: dGVzdAo=
  password: dGVzdHBhc3N3b3JkCg==
kind: Secret
metadata:
  name: my-secret-1
type: Opaque
---
apiVersion: v1
kind: Pod
metadata:
  name: test-site
  labels:
    app: web
spec:
  containers:
    - name: front-end
      image: nginx
      ports:
        - containerPort: 80
    - name: rss-reader
      image: nickchase/rss-php-nginx:v1
      ports:
        - containerPort: 88
---
apiVersion: v1
data:
  username: dGVzdAo=
  password: dGVzdHBhc3N3b3JkCg==
kind: Secret
metadata:
  name: my-secret-2
type: Opaque
---
</code></pre>

<p><a href="https://github.com/kislyuk/yq" rel="nofollow noreferrer"><code>yq</code></a> can do this (and <a href="https://stedolan.github.io/jq/download/" rel="nofollow noreferrer"><code>jq</code></a> underneath)</p>
<pre><code>pip install yq
</code></pre>
<pre><code>yq --yaml-output 'select(.kind != &quot;Secret&quot;)' input.yaml
</code></pre>
<p>You might need to remove the null document at the end of your example, it caused a little bit of weirdness in the output</p>
<p><em><strong>Note</strong></em> that there is also a different <a href="https://github.com/mikefarah/yq" rel="nofollow noreferrer"><code>yq</code> utility</a> that doesn't seem to do what <code>jq</code> does so I'm not sure how to make that one work.</p>
