Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse a string of a kubectl cmd output in a shell script?

kubectl get nodes -o name gives me the output

node/k8s-control.anything
node/k8s-worker1.anything

I need to get only

control
worker1

as output and want to iterate through these elements

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

for elm in $(kubectl get nodes -o name); do echo "$elm" >> file.txt; done

So the question is how to get the string between node/k8s- and .anything and iterate these in the for loop.

>Solution :

You can for example use cut twice, first to get a part after - and
then to get a part before .:

for elm in $(kubectl get nodes -o name | cut -d- -f2 | cut -d. -f1); do echo "$elm" >> file.txt; done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading