1

How can I wrap paragraphs in plain text with paragraph tags {p} before and {/p} after each paragraph using sed? Each paragraph is separated by blank lines. I can use sed -e 's/^\s*$/<r>/ somefile.txt to find every blank line in the text file, but this will always insert {p} everywhere and I don't quite understand, how to vary them. Also, there's no empty line after the very last paragraph, so it won't do anything for the last one.

Input text:

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.

Required Output:

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}
  • show the input text – RomanPerekhrest May 10 '17 at 9:07
  • added to the question, cheers – Daicem May 10 '17 at 9:15
  • good, you'll get a quick help if you also add the expected result (how it should look) – RomanPerekhrest May 10 '17 at 9:17
  • but the line Section 5. General Information About Project Gutenberg-tm electronic works. is also a paragraph, why it's not wrapped in your output? – RomanPerekhrest May 10 '17 at 9:22
  • because it's the title of text – Daicem May 10 '17 at 9:23
0

As you originally requested an sed solution, I append one:

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

Explanation

  • line 1: Append non-empty lines to the hold buffer (1st line copied instead of appended to avoid starting with newline). Continue for empty lines or end of file.
  • line 2: Ignore buffers without text to handle multiple empty lines or empty lines at the end of the buffer
  • line 3: If there is an opening tag, add a closing tag. Then print.
  • line 4: Fill the hold buffer with a new opening tag.
  • Thanks, it works in an interactive mode, but what if I need to give an input of a file as a parameter? Looks a bit confusing... – Daicem May 10 '17 at 12:41
  • Just add the filename after the script. Edited the answer to show. And yes, sed scripts are sometimes hard to read. I hope my explanation helps you understand – Philippos May 10 '17 at 12:58
0

I would suggest awk approach:

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

The output:

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's record separator, defaults to the newline \n

NR>1 - skips the first header line

NF - points to total number of fields of the line (considering non-empty lines)

  • \n is the default record separator. Use RS="" for the paragraph mode. gawk -v RS= '{printf "%s", "{p}\n" $0 "\n{/p}" RT}' – Stéphane Chazelas May 10 '17 at 10:09
  • $0 ~ /\S/ (same as /\S/ alone) is GNU-specific. You can use NF (number of fields non-zero) or /[^[:space:]]/ portably – Stéphane Chazelas May 10 '17 at 10:13
  • @StéphaneChazelas, you're right. I've forgotten those moments. Thanks for the hints – RomanPerekhrest May 10 '17 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.