【发布时间】: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>
我不确定 sed 和 awk 是否是用于此目的的错误工具,但首选类似工具/解决方案。
输入
<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’s email address, the recipient’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’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’s email address, the recipient’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’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 > edited.html棘手的部分是找出模式,这可能需要一些试验和错误 -
您发布的 sn-p 是示例输入还是预期输出?在任何情况下 - 发布可测试的样本输入和给定输入的精确预期输出并明确说明哪个是哪个。请记住,我们正在寻找的东西不仅可以澄清您的问题,而且我们可以测试潜在的解决方案。
-
应该可以使用 sed 来完成,可能带有 Perl 标志。不过我不知道 sed 命令行。