如何從命令輸出中 grep 字串

如何從命令輸出中 grep 字串
NAME                READY     STATUS    RESTARTS   AGE
grepme              1/1       Running   0          20h
grepmetoo           1/1       Running   0          19h

結果:

grepme
grepmetoo

如何grep“NAME”下的所有內容並刪除其他內容?

答案1

使用

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

正如您所提到的grep,您也可以使用

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

但我更喜歡上述之一,因為它更難閱讀。
cut解決方案具有最好的性能,也是csvcut迄今為止最差的。

答案2

首先考慮僅輸出所需的數據:

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

或者

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

(拉自這個堆疊溢位線程kubectl 概述

相關內容