<p>I am learning Kubernetes Label Selector at the URL below.</p>
<p><a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/</a></p>
<h3>Set-based requirement</h3>
<blockquote>
<p>Set-based label requirements allow filtering keys according to a set of values. Three kinds of operators are supported: in, notin and exists (only the key identifier). For example:</p>
</blockquote>
<pre><code>! partition
</code></pre>
<blockquote>
<p>The fourth(This) example selects all resources without a label with key partition; no values ​​are checked.</p>
</blockquote>
<p>My assumption is that if I specify &quot;!test&quot;, all pods will be output,
I think that nothing is output when &quot;!app&quot; is specified.</p>
<p>I want to exclude the key and output as in the example described here, but the expected result was not obtained.</p>
<pre><code>My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod -l! app
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !app
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l &quot;!app&quot;
-bash: !app: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l &quot;!test&quot;
-bash: !test: event not found
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l !test
-bash: !test: event not found

</code></pre>
<p>The result of executing the --show-labels command is as follows.</p>
<pre><code>My @ Pc: ~ / Understanding-K8s / chap04 $ kubectl get pod --show-labels
NAME          READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod-a   1/1     Running   0          89m   app=photo-view,env=stage
nginx-pod-b   1/1     Running   0          89m   app=imagetrain,env=test
nginx-pod-c   1/1     Running   0          89m   app=prediction,env=test
nginx-pod-d   1/1     Running   0          89m   app=photo-view,env=stage
nginx-pod-e   1/1     Running   0          89m   app=imagetrain,env=stage
nginx-pod-f   1/1     Running   0          89m   app=photo-view,env=prod
</code></pre>
<p>How can I exclude the intended keys such as app and env?</p>
<hr />
<p>tyzbit has given me the answer, and I have tried.
I couldn't describe the result well in the comment, so I will add it here.</p>
<pre><code>My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!app'
No resources found in default namespace.
My@Pc:~/Understanding-K8s/chap04$ kubectl get pod -l '!test'
NAME          READY   STATUS    RESTARTS   AGE
nginx-pod-a   1/1     Running   0          162m
nginx-pod-b   1/1     Running   0          162m
nginx-pod-c   1/1     Running   0          162m
nginx-pod-d   1/1     Running   0          162m
nginx-pod-e   1/1     Running   0          162m
nginx-pod-f   1/1     Running   0          162m
</code></pre>
<hr />
<p>Girdhar Singh Rathore teaches me AWK and jsonpath.</p>
<p>(Maybe, I've written the wrong format...)</p>
AWK
<pre><code>My@Pc:~$ kubectl get pods --show-labels | awk  '$6 !~/app/ {print ;}'
NAME          READY   STATUS    RESTARTS   AGE   LABELS

My@Pc:~$ kubectl get pods --show-labels | awk  '$6 !~/test/ {print ;}'
NAME          READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod-a   1/1     Running   0          18m   app=photo-view,env=stage
nginx-pod-d   1/1     Running   0          18m   app=photo-view,env=stage
nginx-pod-e   1/1     Running   0          18m   app=imagetrain,env=stage
nginx-pod-f   1/1     Running   0          18m   app=photo-view,env=prod
</code></pre>
jsonpath
<pre><code>My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {&quot;\n&quot;} {end}' | awk '$2 !~/app/ {print $1}'

My@Pc:~$ kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {&quot;\n&quot;} {end}' | awk '$2 !~/test/ {print $1}'
nginx-pod-a
nginx-pod-d
nginx-pod-e
nginx-pod-f
</code></pre>

<p>You can use <code>awk</code></p>
<pre><code>kubectl get pods --show-labels | awk  '$6 !~/app/ {print ;}'
</code></pre>
<p>or <code>-o jsonpath</code></p>
<pre><code>kubectl get pods -o jsonpath='{range .items[*]} {.metadata.name} {.metadata.labels} {&quot;\n&quot;} {end}' | awk '$2 !~/app/ {print $1}'
</code></pre>
<p>or selector</p>
<pre><code>kubectl get pods -l '!app'
</code></pre>
