![コマンド出力から文字列をgrepする方法](https://rvso.com/image/154438/%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E5%87%BA%E5%8A%9B%E3%81%8B%E3%82%89%E6%96%87%E5%AD%97%E5%88%97%E3%82%92grep%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
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 の概要)