![명령 출력에서 문자열을 grep하는 방법](https://rvso.com/image/154438/%EB%AA%85%EB%A0%B9%20%EC%B6%9C%EB%A0%A5%EC%97%90%EC%84%9C%20%E2%80%8B%E2%80%8B%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%84%20grep%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
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 개요)