패턴에 따라 특정 단락을 출력하는 방법은 무엇입니까?

패턴에 따라 특정 단락을 출력하는 방법은 무엇입니까?

아래에 샘플 파일이 있습니다.

# This is a test file. This is a test file. This is a test file  
This is a test file. This is a test file. This is a test file.  
This is a test file.

# Need to output just this paragraph.Need to output just this paragraph.  
Need to output just this paragraph TOO. Need to output just this paragraph.  
Need to output just this paragraph.

"#"부터 시작하여 단락의 마지막 문장까지 두 번째 단락만 출력해야 합니다.

패턴을 기반으로 어떻게 grep하고 출력합니까? 파일에 더 많은 단락이 있으면 "TOO"라는 단어가 포함된 단락을 출력하고 싶습니다.

답변1

단락이 빈 줄로 구분된 경우:

awk -v RS= -v ORS='\n\n' /TOO/

빈 레코드 구분 기호( RS)는 다음을 의미합니다.단락 모드여기서 레코드는 일련의 빈 줄로 구분됩니다.

분리 된 경우 #:

awk -v RS='#' '/TOO/ {print RS $0}'

또는

pcregrep -Mo '#[^#]*?TOO[^#]*'
  • -M여러 줄의 경우grep
  • -o일치하는 부분만 출력하려면

답변2

perl -00ne 'print if /TOO/'
  • -00단락 모드를 의미합니다(레코드는 하나 이상의 빈 줄로 구분됩니다).

관련 정보