1

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.

starball
  • 20,030
  • 7
  • 43
  • 238

3 Answers3

1

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\}//"
Phani Kumar
  • 158
  • 5
0

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.

Almero
  • 84
  • 5
0

This code works for me:

kubectl get ns | awk '{print $1}'
Dada
  • 6,313
  • 7
  • 24
  • 43
王小明
  • 1
  • 3