<p>I have the following list of pods:</p>
<pre><code>user@compute:~&gt; kubectl get pods -n test --template '{{range .items}}{{.metadata.name}}{{&quot;\n&quot;}}{{end}}'
test-1
test-2
test-3
abc
cdf
dfg
</code></pre>
<p>I would like to get just the pods which includes <strong>test</strong> in the name. Is any way to accomplish that without using grep or for, but using template or any Kubernetes way of doing it?</p>
<p>Thank you</p>

Albert
<p>The majority of Kubernetes maintainers (as of now), does not intend to make kubectl command more advanced than external tools made especially for data set parsing (e.g. grep, jq), and suggest still to use external tools in use case similar to yours. There is a PR planned for making this limitation more visible in Docs for end-users.</p>
<p>Replacing output format from --template (go-template format) to --jsonpath would make theoretically possible to achieve your goal. But as it happens regex filter is not supported, and such a command results in following error:</p>
<pre><code>error: error parsing jsonpath {.items[?(@.metadata.name=~/^test$/)].metadata.name}, unrecognized character in action: U+007E '~'
</code></pre>
<p>You check and follow the issue discussed here:
<a href="https://github.com/kubernetes/kubernetes/issues/72220" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/72220</a></p>
<p>Having said that, for now you you can give a try with jq instead grep command:</p>
<pre><code>k get po -n monitoring -o json | jq '.items[] | select(.metadata.name|test(&quot;prom&quot;))| .metadata.name'

 
&quot;prometheus-k8s-0&quot;
&quot;prometheus-k8s-1&quot;
&quot;prometheus-operator-65c77bdd6c-lmrlf&quot;
</code></pre>
<p>or use the only known to me workaround to achieve similar result w/o grep, use this very unattractive command:</p>
<pre><code> k set env po/prometheus-k8s-0 -n monitoring -c prometheus-config* --dry-run=client --list
# Pod prometheus-k8s-0, container prometheus-config-reloader
# POD_NAME from field path metadata.name
</code></pre>
<p>*this command uses the fact that '-c' accept wildcards, moreover I'm assuming that container names are consistent with Pod names</p>
