명령 출력에서 ​​문자열을 grep하는 방법

명령 출력에서 ​​문자열을 grep하는 방법
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 개요)

관련 정보