grep 검색 + 다음 줄

grep 검색 + 다음 줄

명령 을 사용하여 grep다음과 같이 필요한 텍스트를 찾았습니다.

grep 'C02' ~/temp/log.txt

이제 원하는 문자열을 찾을 때마다 찾은 문자열 다음에 오는 줄을 인쇄하고 싶습니다.

abc예를 들어, 원하는 텍스트가 이고 12행에 있다고 가정하면 abc13행도 인쇄하고 싶습니다.

답변1

Linux 시스템을 사용하는 경우 다음을 시도해 볼 수 있습니다.

grep -A1 "C02" ~/temp/log.txt


OPTIONS
       -A NUM, --after-context=NUM
              Print NUM lines of trailing context after matching lines.  Places a line containing -- between contiguous groups of matches.
       -B NUM, --before-context=NUM
              Print NUM lines of leading context before matching lines.  Places a line containing -- between contiguous groups of matches.
       -C NUM, --context=NUM
              Print NUM lines of output context.  Places a line containing -- between contiguous groups of matches.

다음과 같이 awk를 사용할 수도 있습니다.

awk '/C02/{print;getline;print}' ~/temp/log.txt

관련 정보