Oc get pods - Command to just print pod names
Asked Answered
B

4

14

I want to get a list of just the pod names and the Result should not include the status, number of instances etc.

I am using the command

oc get pods

It prints

Pod1-qawer            Running           1/1           2d
Pod2g-bvch            Running           1/1           3h

Expected result

Pod1-qawer
Pod2g-bvch

How do i avoid the extra details from getting printed

Berate answered 28/8, 2019 at 21:10 Comment(3)
Have you tried oc get pods -o name? Run oc get --help for help strings on all the options.Irick
Thanks, it worked. But it adds a pod/ prefix before the pod names. Is there a way to suppress that too?Berate
Use oc get pod -o template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'. See cookbook.openshift.org/working-with-resource-objects/…Irick
M
24

You can omit the headers with --no-headers and you can use -o custom-columns= to customize the output.

oc get pods -o custom-columns=POD:.metadata.name --no-headers

Example output

$ oc get pods -o custom-columns=POD:.metadata.name --no-headers
goapp-75d9b6bfbf-b5fdh
httpd-58c5c54fff-b97h8
app-proxy-6c8dfb4899-8vdkb
app-64d5985fdb-xjp58
httpd-dd5976fc-rsnhz
Muir answered 28/1, 2020 at 21:38 Comment(2)
In my case I needed the name of running pods and it had to be filtered by a label. To see the available labels: --show-labels=true. Filter for label: -l label=value. Only running pods: --show-all=falseLajoie
hi i've accidentialy downvoted your answer ... Can you make a edit so that I can upvote itCompensable
J
9

Use the oc get <object> -o name syntax, here (for pods):

oc get pods -o name

but it also applies to dc, svc, route, template, ..

Sample output:

pod/m0001-v5-tst-1-b5xfs
pod/m0001-v5-tst-1-mv5zl

note that these object prefixes (here: pod/) are perfectly acceptable for all oc Client Tools commands, so no need to strip the prefixes, they can stay and be processed further, e.g. thus:

$ oc describe $(oc get pods -o name | grep m0001-v5) | grep TAG

      CONTAINER_TAG:               20200430
      CONTAINER_TAG:               20200430

Notice that during such further processing we do not use object names such as pods (i.e. oc describe without pods) to avoid duplication.

Another example:

$ oc delete $(oc get dc,svc,route,is -o name)
service "nginx" deleted
route.route.openshift.io "nginx" deleted
imagestream.image.openshift.io "nginx" deleted
Jaymie answered 3/5, 2020 at 18:4 Comment(1)
it works for almost all oc client tools - except for the copy commandWaechter
I
0

oc get po --no-headers | awk '{print $1}'

Irresoluble answered 28/1, 2020 at 21:30 Comment(0)
I
0

try:

oc get pods |awk -F" " 'print {$0}'

Inborn answered 1/9, 2021 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.