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}
10
  • show the input text May 10, 2017 at 9:07
  • added to the question, cheers
    – Graizer
    May 10, 2017 at 9:15
  • good, you'll get a quick help if you also add the expected result (how it should look) May 10, 2017 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? May 10, 2017 at 9:22
  • because it's the title of text
    – Graizer
    May 10, 2017 at 9:23

2 Answers 2

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.
2
  • 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...
    – Graizer
    May 10, 2017 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, 2017 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)

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .