从出现的下一行开始用一行替换多行

Replace multiple lines by one line starting from the next line of the occurrence

我想用一行替换从出现的下一行开始的多行。

例如,我有一个 html 文件的以下部分:

...
<div class="myclass">
  <p textselect="true">
     one
     two
     three
  </p>
</div>
...

我想结束

...
<div class="myclass">
  <p textselect="true">
     hello
  </p>
</div>
...

...但我需要能够将字符串与 <p textselect="true"> 匹配,而不是与 one 匹配,因为我不知道该字符串是什么 (one)会的。

现在我的解决方案非常糟糕。我在 <div class="myclass"> 之后附加了一个占位符,我删除了占位符和接下来的 3 行,然后我再次附加了我想要的字符串。全部用sed。我需要它与 sedawk.

这可能适合您 (GNU sed):

sed -E '/<p textselect="true">/{:a;N;/<\/p>/!ba;s/(\n\s*).*\n/hello\n/}' file

收集 <p textselect="true"></p> 之间的行,并将中间的字符串替换为 hello

选择:

sed -n '/<p textselect="true">/{p;n;s/\S\+/hello/;h;:a;n;/<\/p>/!ba;H;g};p' file

N.B。两种解决方案都希望至少替换一行。

在每个 Unix 机器上的任何 shell 中使用任何 awk:

$ awk '/<\/p>/{f=0} !f; sub(/<p textselect="true">/,"  hello"){print; f=1}' file
...
<div class="myclass">
  <p textselect="true">
    hello
  </p>
</div>
...

或者如果您的替换字符串 hello 可能包含 &*sub() 的反向引用元字符)则:

$ awk '/<\/p>/{f=0} !f; sub(/<p textselect="true">/,"  "){print [=11=] "hello"; f=1}' file
...
<div class="myclass">
  <p textselect="true">
    hello
  </p>
</div>
...