
NAME READY STATUS RESTARTS AGE
grepme 1/1 Running 0 20h
grepmetoo 1/1 Running 0 19h
結果:
grepme
grepmetoo
「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の概要)