Removing duplicate XML markup with awk

huangapple go评论66阅读模式
英文:

Removing duplicate XML markup with awk

问题

寻找替换两行中的重复实例,例如:

    <\section>
         <\section>

用单个 </section> 条目替换。

输入文件中的空格数量可能有所不同。

如果可以使用sed完成,那就更好。但也许我需要使用awk。

英文:

Looking to replace duplicate instances over two lines such as:

<\section>
     <\section>

with a single </section> entry.

Amount of white space in input file may vary.

If this can be done with sed, all the better. But maybe I need to use awk.

答案1

得分: 1

使用GNU sed 的 -E-z\s

$ sed -Ez 's:(<\\section>)\s*\n\s*:</section>:g' 文件
</section>

如果不希望在两个 <\section> 之间有多个空行或空白行,将每个 \s 替换为 [[:blank:]]。它还会一次性将整个输入读入内存。

英文:

Using GNU sed for -E, -z and \s:

$ sed -Ez 's:(<\\section>)\s*\n\s*:</section>:g' file
</section>

That would allow multiple empty lines or lines of blanks between 2 occurrences of <\section>, if that's undesirable then replace each \s with [[:blank:]]. It will also read the whole of the input into memory at once.

答案2

得分: 0

像这样可能会起作用(GNU sed):

sed -Ez 's:(<\\section>)[[:space:]]+:</section>:'
英文:

Something like this might work (GNU sed):

sed -Ez 's:(<\\section>)[[:space:]]+:</section>:'

答案3

得分: 0

这可能适用于您(GNU sed):

sed -E 'N;s/(<\\section>)\s*\n\s*/<\/section>/;P;D' file

打开一个两行窗口,并使用模式匹配替换所需的字符串。

英文:

This might work for you (GNU sed):

sed -E &#39;N;s/(&lt;\\section&gt;)\s*\n\s*/&lt;\/section&gt;/;P;D&#39; file

Open a two line window and using pattern matching substitute the required string.

huangapple
  • 本文由 发表于 2023年4月17日 17:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定