Home > Software engineering >  Kubectl Namespace script to filter only name
Kubectl Namespace script to filter only name

Time:05-20

I'm trying to use Kubectl get namespaces command it is fetching the data.

kubectl get namespace
NAME              STATUS   AGE
default           Active   1d
kube-node-lease   Active   1d
kube-public       Active   1d
kube-system       Active   1d

but I want to filter it with name only. So when u run the script it should show like this.

kubectl get namespace
NAME              
default           
kube-node-lease  
kube-public       
kube-system    

I've tried some powershell command but it is not working out for me. Anykinda help will be appreceated. Thanks !

CodePudding user response:

I presume the output of kubectrl is pure text so to manipulate it you will need to either do text parsing (error-prone and not fun) or you need to get the data into a PowerShell object.

ConvertFrom-SourceTable

This will allow you to read and parse the text/string data into a PowerShell object. Then you can simply Select-Object name to get the Name column only.

The second option is to use text/string parsing but for that, you will have to use length detections to extract the columns. An example of that can be found here.

CodePudding user response:

kubectl get ns | awk '{print $1}'

the code above is works for me . you can try it

CodePudding user response:

Try any one of the command

kubectl get namespace --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'
kubectl get namespace | awk '{print $1}'
kubectl get namespace --no-headers -o custom-columns=":metadata.name"
kubectl get namespace -o=name |  sed "s/^.\{10\}//"
  • Related