sed段落标签

戴库斯

如何使用在每个段落{p}前后添加带有段落标签的纯文本{/p}段落sed每个段落用空白行分隔。我可以使用它sed -e 's/^\s*$/<r>/ somefile.txt来查找文本文件中的每个空白行,但这总是会在所有位置插入{p},我不太了解如何更改它们。此外,最后一段之后没有空行,因此最后一段不会做任何事情。

输入文本:

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}
菲力浦斯

当您最初要求sed解决方案时,我附上一个:

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

解释

  • 第1行:将非空行追加到保持缓冲区(复制第一行而不是追加第一行,以避免以换行开头)。继续输入空行或文件结尾。
  • 第2行:忽略无文本的缓冲区,以处理多个空行或缓冲区末尾的空行
  • 第3行:如果有开始标签,请添加结束标签。然后打印。
  • 第4行:用新的开始标记填充保持缓冲区。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章