sed 단락 태그

sed 단락 태그

각 단락 {p}앞뒤에 단락 태그를 사용하여 단락을 일반 텍스트로 묶는 방법은 무엇입니까? 각 단락은 빈 줄로 구분됩니다. 텍스트 파일에서 모든 빈 줄을 찾는 데 사용할 수 있지만 항상 {p}가 모든 곳에 삽입되며 이를 변경하는 방법을 잘 모르겠습니다. 또한 마지막 문단 뒤에는 빈 줄이 없으므로 마지막 문단에서는 아무 일도 하지 않습니다.{/p}sedsed -e 's/^\s*$/<r>/ somefile.txt

텍스트 입력:

Section 5. General Information About Project Gutenberg-tm electronic
works.

Description

Professor Michael S. Hart is the originator of the Project Gutenberg-tm
concept of a library of electronic works that could be freely shared
with anyone.

Project Gutenberg-tm eBooks are often created from several printed
editions, all of which are confirmed as Public Domain in the U.S. unless
a copyright notice is included.

원하는 출력:

Section 5. General Information About Project Gutenberg-tm electronic
works.
{p}
Description
{/p}
{p}
Professor Michael S. Hart is the originator of the Project Gutenberg-tm
concept of a library of electronic works that could be freely shared
with anyone.
{/p}
{p}
Project Gutenberg-tm eBooks are often created from several printed
editions, all of which are confirmed as Public Domain in the U.S. unless
a copyright notice is included.
{/p}

답변1

처음에 솔루션을 요청하셨으므로 sed다음 중 하나를 첨부합니다.

sed '/./{H;1h;$! d}
g;/{p}$/d
s#^{p}.*#&\n{/p}#;p
s/.*/{p}/;h;d' somefile.txt

설명하다

  • 라인 1: 비어 있지 않은 라인을 보유 버퍼에 추가합니다(개행으로 시작하지 않으려면 첫 번째 라인을 추가하는 대신 복사). 빈 줄이나 파일 끝을 계속 처리합니다.
  • 2행: 여러 개의 빈 행 또는 버퍼 끝에 있는 빈 행을 처리하기 위해 텍스트가 없는 버퍼를 무시합니다.
  • 3행 : 여는 태그가 있는 경우 종료 태그를 추가합니다. 그런 다음 인쇄하십시오.
  • 4행 : 새로운 시작 태그로 보유 버퍼를 채웁니다.

답변2

내가 제안 할게방법:

awk 'NR>1 && NF{$0="{p}" RS $0 RS "{/p}"}1' file

산출:

Section 5. General Information About Project Gutenberg-tm electronic works.

{p}
Description
{/p}

{p}
Professor Michael S. Hart is the originator of the Project Gutenberg-tm concept of a library of electronic works that could be freely shared with anyone. For thirty years, he produced and distributed Project Gutenberg-tm eBooks with only a loose network of volunteer support.
{/p}

{p}
Project Gutenberg-tm eBooks are often created from several printed editions, all of which are confirmed as Public Domain in the U.S. unless a copyright notice is included. Thus, we do not necessarily keep eBooks in compliance with any particular paper edition.
{/p}

RS- awk의 레코드 구분 기호, 기본값은 개행입니다.\n

NR>1- 첫 번째 것을 건너 뛰세요머리글철사

NF- 행을 가리키는 총 필드 수(비어 있지 않은 행 고려)

관련 정보