<p>I am trying to collect IP addresses of the pods which has 3 network interfaces attached to. From  the output of <code>kubectl get pod .. -o yaml</code>, I would like to collect the IPs of 2nd and 3rd interfaces including pod-name.</p>
<p><code>kubectl describe pod</code> data:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    annotations:
      k8s.v1.cni.cncf.io/network-status: |-
        [{
            &quot;name&quot;: &quot;default-cni-network&quot;,
            &quot;ips&quot;: [
                &quot;10.244.160.191&quot;
            ],
            &quot;default&quot;: true,
            &quot;dns&quot;: {}
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov1&quot;,
            &quot;interface&quot;: &quot;net1&quot;,
            &quot;ips&quot;: [
                &quot;10.56.217.100&quot;
            ],
            &quot;mac&quot;: &quot;6a:58:82:8a:93:7b&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:02.7&quot;
                }
            }
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov2&quot;,
            &quot;interface&quot;: &quot;net2&quot;,
            &quot;ips&quot;: [
                &quot;10.56.218.100&quot;
            ],
            &quot;mac&quot;: &quot;66:a2:70:eb:5e:04&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:14.0&quot;
                }
            }
        }]
      k8s.v1.cni.cncf.io/networks: minio-sriov1,minio-sriov2
      k8s.v1.cni.cncf.io/networks-status: |-
        [{
            &quot;name&quot;: &quot;default-cni-network&quot;,
            &quot;ips&quot;: [
                &quot;10.244.160.191&quot;
            ],
            &quot;default&quot;: true,
            &quot;dns&quot;: {}
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov1&quot;,
            &quot;interface&quot;: &quot;net1&quot;,
            &quot;ips&quot;: [
                &quot;10.56.217.100&quot;
            ],
            &quot;mac&quot;: &quot;6a:58:82:8a:93:7b&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:02.7&quot;
                }
            }
        },{
            &quot;name&quot;: &quot;minio-tenant/minio-sriov2&quot;,
            &quot;interface&quot;: &quot;net2&quot;,
            &quot;ips&quot;: [
                &quot;10.56.218.100&quot;
            ],
            &quot;mac&quot;: &quot;66:a2:70:eb:5e:04&quot;,
            &quot;dns&quot;: {},
            &quot;device-info&quot;: {
                &quot;type&quot;: &quot;pci&quot;,
                &quot;version&quot;: &quot;1.0.0&quot;,
                &quot;pci&quot;: {
                    &quot;pci-address&quot;: &quot;0000:b1:14.0&quot;
                }
            }
        }]
      kubernetes.io/psp: node-exporter
      meta.helm.sh/release-name: minio-tenant
      meta.helm.sh/release-namespace: minio-tenant
      min.io/revision: &quot;0&quot;
    creationTimestamp: &quot;2022-04-27T08:09:45Z&quot;
    generateName: minio-tenant-ss-0-
    labels:
      app: minio
      app.kubernetes.io/managed-by: Helm
      controller-revision-hash: minio-tenant-ss-0-7976bbfbc6
      statefulset.kubernetes.io/pod-name: minio-tenant-ss-0-0
  spec:
    affinity:
    containers:
    - args:
      - :9090
      env:
      - name: MINIO_UPDATE
</code></pre>
<p>Using this regular expression:</p>
<pre class="lang-regex prettyprint-override"><code>networks-status:[\s\S]*?
(?:&quot;)(name)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s]*)
(?:&quot;)(interface)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s\S]*?)
(?:&quot;)(ips)(?:&quot;:\s*\[\s*&quot;)([\S]*)(?:&quot;)
[\s\S]*?(pod-name)(?::\s)([\S]*)
</code></pre>
<p>I would like to get the following output:</p>
<pre class="lang-none prettyprint-override"><code>name
minio-tenant/minio-sriov1
interface
net1
ips
10.56.217.100
name
minio-tenant/minio-sriov2
interface
net2
ips
10.56.218.100
pod-name
minio-tenant-ss-0-0
</code></pre>
<p>It gets me:</p>
<pre class="lang-none prettyprint-override"><code>name
minio-tenant/minio-sriov1
interface
net1
ips
10.56.217.100
pod-name
minio-tenant-ss-0-0
</code></pre>
<p>There is missing portion of IP:</p>
<pre class="lang-yaml prettyprint-override"><code>name
minio-tenant/minio-sriov2
interface
net2
ips
10.56.218.100
</code></pre>
<p>If I use <code>*</code> or <code>+</code> to repeat on the data section, I am getting unexpected result.</p>
<pre class="lang-regex prettyprint-override"><code>networks-status:[\s\S]*?
(
(?:&quot;)(name)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s]*)
(?:&quot;)(interface)(?:&quot;:\s*&quot;)([\S]*)(?:&quot;,[\s\S]*?)
(?:&quot;)(ips)(?:&quot;:\s*\[\s*&quot;)([\S]*)(?:&quot;)
)*
[\s\S]*?(pod-name)(?::\s)([\S]*)
</code></pre>
<p>Can you give some hints how I can get the expected results?</p>

spark
<p>As I said in the comments, <code>jq</code> is the tool of your choise here.</p>
<pre class="lang-sh prettyprint-override"><code>$ kubectl get pod &lt;name&gt; -o json | jq -r '.items[].metadata.annotations.&quot;k8s.v1.cni.cncf.io/network-status&quot; | fromjson | del(.[0]) | .[] | .name + &quot;\t&quot; + .interface + &quot;\t&quot; + .ips[]'
</code></pre>
<p>First we get the value of the annotation by <code>.items[].metadata.annotations.&quot;k8s.v1.cni.cncf.io/network-status&quot;</code> which returns a string with json in it. So we use <code>fromjson</code> to convert it from json and get an array. We delete the first entry <code>del(.[0])</code>, because you wrote you only want the 2nd and 3rd and in the end print the name, the interface and the ip separated with a tab for each element <code>.[] | .name + &quot;\t&quot; + .interface + &quot;\t&quot; + .ips[]</code>.</p>
<p>This will output</p>
<pre><code>minio-tenant/minio-sriov1   net1    10.56.217.100
minio-tenant/minio-sriov2   net2    10.56.218.100
</code></pre>
