<p>Having YAML document something like:</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: allow-webhooks
</code></pre>
<p>I am trying to get something like</p>
<pre class="lang-yaml prettyprint-override"><code>---
apiVersion: **networking.k8s.io/v1beta1**
kind: NetworkPolicy
metadata:
  name: allow-scraping
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: allow-webhooks
</code></pre>
<p>So basically get document, if document has <code>kind: NetworkPolicy</code> then patch <code>apiVersion: networking.k8s.io/v1beta1</code>.</p>
<p>Ideally one liner, ideally with yq v4, but other solutions will be helpful too.</p>

<p>With <a href="https://github.com/mikefarah/yq" rel="noreferrer">mikefarah/yq</a> on versions beyond 4, you could do a <code>select</code> and update <code>|=</code> operation on the required document</p>
<pre class="lang-sh prettyprint-override"><code>yq e 'select(.kind == &quot;NetworkPolicy&quot;).apiVersion |= &quot;networking.k8s.io/v1beta1&quot;' yaml
</code></pre>
<p>The above works fine on <code>yq version 4.6.0</code>. Use the <code>-i</code> flag to replace the file in-place.</p>
