<p>I want to extract a pod name from a list of pods available on kubernetes.
For example, for the following command</p>
<blockquote>
<p>kubectl get pods -n namespace</p>
</blockquote>
<pre><code>NAME          READY   STATUS            RESTARTS   AGE
pod1          1/1     Running           2          46d
pod2          1/1     Running           0          46d
test-pod3-yy  0/1     ImagePullBackOff  0          338d
test-pod3-xx  1/1     Running           0          255d
</code></pre>
<p>I want to extract pod <code>test-pod3-xx</code> using shell script. Currently this is the command that I'm using</p>
<pre><code>POD_NAME=$(kubectl get pods -n namespace | grep testpod-3 | cut -d' ' -f1)
</code></pre>
<p>With this I get both the pods <code>test-pod3-yy</code> and <code>test-pod3-xx</code> but I want to extract the pod that is in running state. How can I do that?</p>

VIOLET
<p>You can use the <code>field-selector</code> and check for running only:</p>
<p><code>--field-selector=status.phase=Running</code></p>
<p>You could also use the <code>-o name</code> flag, to get only the names. With that you'll get:</p>
<pre class="lang-bash prettyprint-override"><code>$ kubectl get pods -n namespace -o name --field-selector=status.phase=Running
pod/pod1
pod/pod2
pod/test-pod3-xx
</code></pre>
