![如何從命令輸出中 grep 字串](https://rvso.com/image/154438/%E5%A6%82%E4%BD%95%E5%BE%9E%E5%91%BD%E4%BB%A4%E8%BC%B8%E5%87%BA%E4%B8%AD%20grep%20%E5%AD%97%E4%B8%B2.png)
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 概述)