【问题标题】:Search and Replace RANGE Pattern搜索和替换 RANGE 模式
【发布时间】:2015-10-09 23:18:22
【问题描述】:

想象多个 HTML 文件与所有剩余的格式、标签等合并——别管为什么——应该使用什么工具从随后合并的 html 文件的开头行搜索,即<!doctype html>...<h1> 标头的开头?该范围模式应改为水平线。

---END OF PREV MERGED FILE---
---BEGIN SEARCH/REPLACE HERE---
<!doctype html>
        <!--[if !IE]>
        <html class="no-js non-ie" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 7 ]>
        <html class="no-js ie7" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 8 ]>
        <html class="no-js ie8" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
        <!--[if IE 9 ]>
---HEAD,META,ETC---
---END SEARCH/REPLACE HERE---
<h1>TITLE OF NEXT MERGED FILE</h1>

我不确定 sedawk 是否是用于此目的的错误工具,但首选类似工具/解决方案。


输入

<li><strong>email_from = root@localhost</strong>, <strong>email_to = root</strong>, <strong>email_host = localhost</strong> defines respectively when the message is a mail the originator&#8217;s email address, the recipient&#8217;s
 email address and the host to which the mail is sent.<strong><br />
 30658  </strong></li>
 30659  </ul>
 30660  <p>Source: <a title="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7" href="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7">Linuxaria&#8217;s website</a>.</p>
 30661                                                                          </div><!-- end of .post-entry -->

 30662

 30663  <div class="post-edit"></div>
 30664                                                          </div><!-- end of #post-4116 -->
 30665
 30666




 30667          <!doctype html>
 30668          <!--[if !IE]>
 30669          <html class="no-js non-ie" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30670          <!--[if IE 7 ]>
 30671          <html class="no-js ie7" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30672          <!--[if IE 8 ]>
 30673          <html class="no-js ie8" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30674          <!--[if IE 9 ]>
 30675          <html class="no-js ie9" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]-->
 30676          <!--[if gt IE 9]><!-->
 30677  <html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#"> <!--<![endif]-->
 30678          <head>

 30679                  <meta charset="UTF-8"/>
 30680                  <meta name="viewport" content="width=device-width, initial-scale=1.0">

 30681                  <title>something something</title>

 30682                  <link rel="profile" href="http://gmpg.org/xfn/11"/>
 30683                  <link rel="pingback" href="www.example.com"/>

 30684
 30685          <h1 class="entry-title post-title">Something Something</h1>

预期输出

<li><strong>email_from = root@localhost</strong>, <strong>email_to = root</strong>, <strong>email_host = localhost</strong> defines respectively when the message is a mail the originator&#8217;s email address, the recipient&#8217;s
     email address and the host to which the mail is sent.<strong><br />
     30658  </strong></li>
     30659  </ul>
     30660  <p>Source: <a title="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7" href="http://linuxaria.com/howto/enabling-automatic-updates-in-centos-7-and-rhel-7">Linuxaria&#8217;s website</a>.</p>
     30661                                                                          </div><!-- end of .post-entry -->

     30662

     30663  <div class="post-edit"></div>
     30664                                                          </div><!-- end of #post-4116 -->


    <hr />




     30685          <h1 class="entry-title post-title">Something Something</h1>

【问题讨论】:

  • 您看过手册页吗? sed 用于搜索和替换,支持正则表达式。 awk 更适合处理柱状文本文件。我想你想要sed
  • 是的,问题在于sed 我不知道确切的行号;用awk,html格式是一个PITA;因此,即使查看了 众多 其他与我需要的距离相差一、二或三度的示例,我也不确定如何继续。
  • sed 不需要行号,它类似于:sed 's/pattern/replace/g' file.html &gt; edited.html 棘手的部分是找出模式,这可能需要一些试验和错误
  • 您发布的 sn-p 是示例输入还是预期输出?在任何情况下 - 发布可测试的样本输入和给定输入的精确预期输出并明确说明哪个是哪个。请记住,我们正在寻找的东西不仅可以澄清您的问题,而且我们可以测试潜在的解决方案。
  • 应该可以使用 sed 来完成,可能带有 Perl 标志。不过我不知道 sed 命令行。

标签: html regex bash awk sed


【解决方案1】:

这似乎是你想要的:

awk '/<!doctype html>/{f=1;print "    <hr />";} /<h1 class=/{f=0;} !f' input >output

工作原理

  • /&lt;!doctype html&gt;/{f=1;print " &lt;hr /&gt;";}

    当我们到达包含&lt;!doctype html&gt; 的行时,这会将标志f 设置为1 以表示我们应该停止打印。然后,我们打印水平线。

  • /&lt;h1 class=/{f=0;}

    当我们到达包含&lt;h1 class= 的行时。将标志 f 设置为 0 以表示我们可以继续打印。

  • !f

    如果f0,这会导致打印当前行。

    更详细地说,!f 是一个条件。当条件为真时,awk 执行一个动作。由于未指定任何操作,awk 将执行其默认操作,即打印该行。 ! 是 awk 的否定符号。因此,当f 为假(0)时,!f 为真并打印该行。

保留第一个 doctype 标签

假设我们首先要删除所有 doctype 标签 except。在这种情况下:

awk '/<!doctype html>/{count++; if (count>1){f=1; print "    <hr />";}} /<h1 class=/{f=0;} !f' input

这是通过添加另一个变量count 来实现的,该变量跟踪我们看到的文档类型标签的数量。标志f 设置为1 只有在我们看到多个doctype 标记之后。

为了演示以上内容,让我们使用这个输入文件:

$ cat input2
miscellaneous stuff
30667          <!doctype html>
30668          something
30669          <h1 class="entry-title post-title">Something Something</h1>
More stuff
30667          <!doctype html>
30668          something 2
30669          <h1 class="entry-title post-title">Something Something</h1>
Still More stuff
30667          <!doctype html>
30668          something 3
30669          <h1 class="entry-title post-title">Something Something</h1>
Stuff at end

该命令产生的输出是:

$ awk '/<!doctype html>/{count++; if (count>1){f=1; print "    <hr />";}} /<h1 class=/{f=0;} !f' input2
miscellaneous stuff
30667          <!doctype html>
30668          something
30669          <h1 class="entry-title post-title">Something Something</h1>
More stuff
    <hr />
30669          <h1 class="entry-title post-title">Something Something</h1>
Still More stuff
    <hr />
30669          <h1 class="entry-title post-title">Something Something</h1>
Stuff at end

【讨论】:

  • 感谢f 标志和详尽的解释。那是 awk 语言还是正则表达式的一部分?
  • 欢迎您。 f 只是一个 awk 变量。与 sed 不同,awk 允许程序按名称定义变量并操作它们的值。我们可以选择任何我们喜欢的名字。
  • 看来我说得太早了;有没有办法告诉awk 跳过文件开头的第一个结果?我记得有关 BEGIN 选项的一些内容,但在这种情况下,它必须与此相反。
  • @ILMostro_7 我在答案中添加了代码以保留第一个 doctype 标记。看看吧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 2011-08-16
  • 2016-03-15
  • 2013-10-17
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多