kubectl format output of get-contexts

4/24/2019

I want to format the output of the following command:

kubectl config get-contexts

so add a delimiter so i can parse it, i've tried YAML And Json but they aren't supported.

How can I format the data to be as follows:

CURRENT,NAME,CLUSTER,AUTHINFO,NAMESPACE,
,name1,cluster1,,clusterUser,,
*,name2,cluster2,clusterUser,,
-- Mr Giggles
kubectl
kubernetes

1 Answer

4/24/2019

You can use linux sed to rearrange the data to be as follows:

[root@]# kubectl config get-contexts | tr -s " " | sed -e "s/ /,/g"
CURRENT,NAME,CLUSTER,AUTHINFO,NAMESPACE
*,kubernetes-admin@kubernetes,kubernetes,kubernetes-admin,
-- Prafull Ladha
Source: StackOverflow