
NAME READY STATUS RESTARTS AGE
grepme 1/1 Running 0 20h
grepmetoo 1/1 Running 0 19h
結果:
grepme
grepmetoo
「NAME」の下のすべてを grep して、それ以外を削除するにはどうすればよいでしょうか?
答え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
(抜粋)このStack Overflowのスレッドそしてそのkubectl の概要)