![패턴에 따라 특정 단락을 출력하는 방법은 무엇입니까? [복사]](https://linux55.com/image/95051/%ED%8C%A8%ED%84%B4%EC%97%90%20%EB%94%B0%EB%9D%BC%20%ED%8A%B9%EC%A0%95%20%EB%8B%A8%EB%9D%BD%EC%9D%84%20%EC%B6%9C%EB%A0%A5%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%EB%B3%B5%EC%82%AC%5D.png)
다음과 같은 샘플 파일이 있습니다.
# 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
단락 모드(하나 이상의 빈 줄로 구분된 레코드)를 나타냅니다.