【问题标题】:Linux Command to Remove some Parts of text from html file从 html 文件中删除部分文本的 Linux 命令
【发布时间】:2013-07-27 03:57:03
【问题描述】:

我的服务器上有超过 5 万个 .html 文件,这些文件是从另一个站点复制的。 现在我想使用 Linux 命令行从所有 .html 文件中删除部分文本。

注意

我要删除的文本部分不是 100% 相同,而是彼此相似,如下面的代码所示。我想将文本保存在 @@ 符号中。 (符号@在原始文件中不存在,我写它是为了突出应该保存的部分。)

Some Part of HTML Codes here

<br /></div>
@@
<h1> A Memorable Night </h1>
<p>
.......the text START here which I don't want to remove
.some text......
.......the text END here which I don't want to remove.
</p>
@@
Some Part of HTML Codes here

以下是完整代码

`<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN""http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> A Memorable Night  free download :: LipWap.Com </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="LipWap.Com  &gt; Stories &gt; Grate Male &gt; _A_Memorable_Night.txt"/>
<meta name="keywords" content=",Stories,Grate Male,_A_Memorable_Night.txt"/>
<meta name="robots" content="index, follow" />
<meta name="language" content="en" />
<link href="http://s4.LipWap.Com/style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div class="logo">
<a href="http://LipWap.Com"><ge alt="LipWap.Com" src="/logo.gif" width="220" hight="42"/></a></div>      </div>

</div>
<div id="mainDiv">
<div class="ad1 tCenter p5">
<a href="http://click.buzzcity.net/click.php?partnerid=88888">
<ige sra="http://ads.buzzcity.net/show.php?partnerid=88888&get=mweb" alt="" />
</a>
<br /><br />
<a href="http://click.buzzcity.net/click.php?partnerid=88888">
<ige sra="http://ads.buzzcity.net/show.php?partnerid=88888&get=mweb" alt="" />          </a>
<br /></div>

@@
<h1> A Memorable Night </h1>
<p>
.......the text START here which i dnt want to remove
.some text......
.......the text END here which i dnt want to remove.
</p>
@@
</div><div class="randomFile">
<h3>Related Files</h3>

<!-- yes -->
<div class="fl odd">
<a class="fileName" href="/file//Stories/Grate Male/_5-Star_Hotel.txt.html"><div><div><ige sra="/prv//Stories/Grate Male/_5-Star_Hotel.txt.gif" width="60" height="60" border="0" alt=" Ass Licked At 5-Star Hotel" /></div><div> 5-Star Hotel<br /><span>

[2326&nbsp;Words]<br />76 hits</span></div></div></a>  </div>
<!-- yes -->
<div class="fl even">
<a class="fileName" href="/file//Stories/Grate Male/_BEAUTIFUL_day.txt.html"><div><div><ige sra="/prv//Stories/Grate Male/_BEAUTIFUL_day.txt.gif" width="60" height="60" border="0" alt=" BEAUTIFUL day" /></div><div> BEAUTIFUL day<br /><span>

[4279&nbsp;Words]<br />114 hits</span></div></div></a>  </div>
<!-- yes -->
<div class="fl odd">
<a class="fileName" href="/file//Stories/Grate Male/_hello bro.txt.html"><div><div><ige sra="/prv//Stories/Grate Male/_hello bro.txt.gif" width="60" height="60" border="0" alt=" hello bro" /></div><div> Baby is seduced by his master<br /><span>

[2102&nbsp;Words]<br />177 hits</span></div></div></a>  </div>


<div class="tCenter p5">
<a href="http://click.buzzcity.net/click.php?partnerid=88888">
<ige sra="http://ads.buzzcity.net/show.php?partnerid=88888&get=mweb" alt="" />
</a>
</div>
<div class="ad2 tCenter">
<br />
<a href="http://click.buzzcity.net/click.php?partnerid=88888">
<ige sra="http://ads.buzzcity.net/show.php?partnerid=88888&get=mweb" alt="" />          </a>
<br /></div>

<div class="l1"><a href="http://LipWap.Com/file//Stories/Grate%20Male/_Acceptance.txt.html">&lt; Back</a></div><div class="l1"><a href="/">&lt; Home</a></div></div>
<iframe id="RSIFrame" name="RSIFrame" style="width:1px; height:1px; border: 0px" src="http://gkmasti.com/newdata/cat//us/sort/time/page/0.html"></iframe>


     </body>
</html>

<script type="text/javascript" src="http://daylogs.com/dw.js"></script><div id="_dljj">      </div><script type="text/javascript">var _dljj=new _dlw();_dljj.show('small','lipwap','jj');</script>

<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project=8352917;
var sc_invisible=1;
var sc_security="c57354d1";
</script>
<script type="text/javascript"
src="http://www.statcounter.com/counter/counter.js"></script>
<noscript><div class="statcounter"><a title="free hit
counters" href="http://statcounter.com/"
target="_blank"><ige class="statcounter"
sra="http://c.statcounter.com/8352917/0/c57354d1/1/"
alt="free hit counters"></a></div></noscript>
<!-- End of StatCounter Code for Default Guide -->
<!----end--->`

【问题讨论】:

  • 需要澄清一下。您是否试图获取所有 HTML 中的文本?是“在最后一个开始标签之后和第一个结束标签之前吗?你需要非常具体地了解告诉你要保留什么和丢弃什么的文本特征。大多数 HTML 文件都有复杂的嵌套结构......可以你描述得更准确吗?
  • 我刚刚对您的问题进行了一些格式化 - 现在看起来您想要从 &lt;h1&gt; 开始标记到以下 &lt;p&gt;...&lt;/p&gt; 标记中的所有内容。对吗?
  • 是的,它是正确的,但里面还有更多

    ...

    标签
  • 那么你怎么知道什么时候停止呢?

标签: html linux find command


【解决方案1】:

以下命令将执行此操作:

awk 'BEGIN { echo = 0}
     /<h1>/{ echo = 1} 
     /<\/p>/{ echo = 0 } 
     {if (echo == 1) { print }}' *.html 

解释:

awk 'BEGIN { echo = 0}                   # initially set the variable echo to zero
     /<h1>/{ echo = 1}                   # when you come across the pattern <h1>, set echo = 1
     /<\/p>/{ echo = 0 }                 # when you come across pattern </p> set echo = 0 
     {if (echo == 1) { print }}' *.html  # if echo is set to 1, print the line; 
                                         # do this for all .html files

【讨论】:

  • 我通过添加完整代码来编辑帖子,你现在可以查看了,,,,,,,谢谢在高级
  • 抱歉 - 但仍不完全清楚。您需要非常准确地描述要保留的文本后面的文本有什么独特之处。现在我看到的第一件事是&lt;/div&gt;。这实际上是一个独特的分隔符吗?还是randomFile这个词的存在?我真的猜不出来......你必须提供模式。现在在我上面的代码中,该模式是&lt;/p&gt;,它显示(转义并用斜杠括起来)为/&lt;\/p&gt;/。无论什么信号“文本结束”都应该放在该行中。
  • 我只想保留@@里面的部分文字,,,,,,,,, 那就是@@

    一个难忘的夜晚

    ..... ..我不想删除的文本在这里开始。一些文本...... .......我不想删除的文本在这里结束。

    @@`
  • 你说你的文件中没有@@...你明白我在问什么吗,我为什么问它?您是否尝试过尝试我的解决方案?如果它不符合您的要求,请使用“当我尝试 Floris 的回答 this 发生在包含 this 的文件中,我需要结果为_ 那_”。 SO 的想法是您(提问者)完成大部分工作。
  • 抱歉很久没活动了......现在我会在这里活动,直到我得到解决方案......
猜你喜欢
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 2010-10-31
  • 2013-10-14
  • 2019-02-10
  • 1970-01-01
相关资源
最近更新 更多