1
NAME                READY     STATUS    RESTARTS   AGE
grepme              1/1       Running   0          20h
grepmetoo           1/1       Running   0          19h

Result:

grepme
grepmetoo

How to grep everything under "NAME" and remove else?

2

Use

command | cut -d' ' -f1 | tail -n+2
# or if delimiter is tab
command | cut -f1 | tail -n+2

# or
command | awk 'NR>1{print $1}'

# or
command | csvcut -d' ' -c NAME | tail -n+2
# or if delimiter is tab
command | csvcut -t -c NAME | tail -n+2

As you mention grep, you could also use

command | grep -o '^[^[:blank:]]*' | tail -n+2

But I'd prefer one of the above as it is way more difficult to read.
The cut solution has the best performance, csvcut by far the worst.

  • Nice simple solution! Marked as correct! – niqo May 31 at 12:08
1

Consider outputting only the desired data to begin with:

kubectl get pods --no-headers=true -o custom-columns=":metadata.name"

or

kubectl get pods --no-headers=true -o name

(Pulled from this Stack Overflow thread and the kubectl overview)

  • Good solution, but since I asked for grep I did'nt make as correct, but great command still! – niqo May 31 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.