Board logo

标题: [已解决]请教sed提取文档两关键词间的内容 [打印本页]

作者: jesqiu     时间: 2008-12-26 16:08    标题: [已解决]请教sed提取文档两关键词间的内容

针对网页html代码,提取中间的内容。测试数据文件见附件(重复部分只是测试用,为了说明同个文档内容,两个关键词包括起来的内容多处出现。实际中是不同的内容,当然两个关键词是相同的)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错题本  QZZN论坛 公务员考试论坛 公务员论坛 
</title>
<meta name="keywords" content="错题本,行政职业能力测试,qzzn">
<meta name="description" content="错题本,&nbsp; &nbsp; &nbsp; [b][color=#ff0000]在我们行测 ..">
<link rel="archives" title="QZZN论坛" href="http://bbs.qzzn.com/simple">
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://bbs.qzzn.com/rss.php?fid=8">

<link rel="stylesheet" type="text/css" href="index.css" media="all">
</head>
<body onkeydown="keyCodes(event);">
<div id="wrapA">
<div id="main">
<form action="http://bbs.qzzn.com/read-htm-tid-9683163-fpage-0-toread--page-1.html" name="delatc" method="post">
<div class="t t2">

<table style="border-top: 0pt none; table-layout: fixed;" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr class="tr1">
<th class="r_two" id="td_11471225" style="border: 0pt none ; padding: 5px 15px 0pt; overflow: hidden; width: 80%;" height="100%" valign="top">
<div class="tiptop">
			<!--隐藏2-->


			<div class="c"></div>
			</div>
			<h1 id="subject_11471225" class="fl">&nbsp;</h1>

			<div id="p_11471225" class="c"></div>

			<div id="mag_11471225" class="tpc_content"><h6 class="quote"><span class="s3 f12 fn">管理提醒:

			(清风淡鼠)

			</span></h6><blockquote class="blockquote">错题原因:题目本来就是错误的<br>提示:大家在做题的时候一定要注意辨别题目的对错,以免错题浪费过多的时间

<span>(08-4-16 15:31)</span>

			</blockquote></div>

			<div class="tpc_content" id="read_11471225">来一个。<br>错题:__某仪仗队排成方阵,第一次排列若干人,结果多余10人,第二次比第一次每排增加3人,结果缺少29人,仪仗队总人数是多少?(&nbsp; &nbsp; )<br>&nbsp; A.4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B.450&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  D.600<br>正确答案:期待高手<br>做错的原因:我认为每一个答案都不对。</div>
<!--复制帖子加链接------>
<script language=javascript>
document.body.oncopy=function(){
event.returnValue=false;
var t=document.selection.createRange().text;
var s="本文转载于,出处:";
window.clipboardData.setData('Text', t+'\r\n'+s);
}
</script>
<table style="border-top: 0pt none; table-layout: fixed;" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr class="tr1">
<th class="r_two" id="td_11471225" style="border: 0pt none ; padding: 5px 15px 0pt; overflow: hidden; width: 80%;" height="100%" valign="top">
<div class="tiptop">
			<!--隐藏2-->


			<div class="c"></div>
			</div>
			<h1 id="subject_11471225" class="fl">&nbsp;</h1>

			<div id="p_11471225" class="c"></div>

			<div id="mag_11471225" class="tpc_content"><h6 class="quote"><span class="s3 f12 fn">管理提醒:

			(清风淡鼠)

			</span></h6><blockquote class="blockquote">错题原因:题目本来就是错误的<br>提示:大家在做题的时候一定要注意辨别题目的对错,以免错题浪费过多的时间

<span>(08-4-16 15:31)</span>

			</blockquote></div>

			<div class="tpc_content" id="read_11471225">来一个。<br>错题:__某仪仗队排成方阵,第一次排列若干人,结果多余10人,第二次比第一次每排增加3人,结果缺少29人,仪仗队总人数是多少?(&nbsp; &nbsp; )<br>&nbsp; A.4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B.450&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  D.600<br>正确答案:期待高手<br>做错的原因:我认为每一个答案都不对。</div>
<!--复制帖子加链接------>
<script language=javascript>
document.body.oncopy=function(){
event.returnValue=false;
var t=document.selection.createRange().text;
var s="本文转载于,出处:";
window.clipboardData.setData('Text', t+'\r\n'+s);
}
</script>

<table style="border-top: 0pt none; table-layout: fixed;" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr class="tr1">
<th class="r_two" id="td_11471225" style="border: 0pt none ; padding: 5px 15px 0pt; overflow: hidden; width: 80%;" height="100%" valign="top">
<div class="tiptop">
			<!--隐藏2-->


			<div class="c"></div>
			</div>
			<h1 id="subject_11471225" class="fl">&nbsp;</h1>

			<div id="p_11471225" class="c"></div>

			<div id="mag_11471225" class="tpc_content"><h6 class="quote"><span class="s3 f12 fn">管理提醒:

			(清风淡鼠)

			</span></h6><blockquote class="blockquote">错题原因:题目本来就是错误的<br>提示:大家在做题的时候一定要注意辨别题目的对错,以免错题浪费过多的时间

<span>(08-4-16 15:31)</span>

			</blockquote></div>

			<div class="tpc_content" id="read_11471225">来一个。<br>错题:__某仪仗队排成方阵,第一次排列若干人,结果多余10人,第二次比第一次每排增加3人,结果缺少29人,仪仗队总人数是多少?(&nbsp; &nbsp; )<br>&nbsp; A.4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B.450&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  D.600<br>正确答案:期待高手<br>做错的原因:我认为每一个答案都不对。</div>
<!--复制帖子加链接------>
<script language=javascript>
document.body.oncopy=function(){
event.returnValue=false;
var t=document.selection.createRange().text;
var s="本文转载于,出处:";
window.clipboardData.setData('Text', t+'\r\n'+s);
}
</script>

</th>
</tr>
</tbody>
</table>
</div>
</form>
</div>
</div>
</body>
</html>
我想提取网页中所有下面内容,规律就是<!--隐藏2-->上面两行开始, 和<!--复制帖子加链接------>中间的内容。因为html代码不好处理,也可以只提取<!--隐藏2-->开始内容,前面的我再处理加进去。
<th class="r_two" id="td_11471225" style="border: 0pt none ; padding: 5px 15px 0pt; overflow: hidden; width: 80%;" height="100%" valign="top">
<div class="tiptop">
                        <!--隐藏2-->


                        <div class="c"></div>
                        </div>
                        <h1 id="subject_11471225" class="fl">&nbsp;</h1>

                        <div id="p_11471225" class="c"></div>

                        <div id="mag_11471225" class="tpc_content"><h6 class="quote"><span class="s3 f12 fn">管理提醒:

                        (清风淡鼠)

                        </span></h6><blockquote class="blockquote">错题原因:题目本来就是错误的<br>提示:大家在做题的时候一定要注意辨别题目的对错,以免错题浪费过多的时间

<span>(08-4-16 15:31)</span>

                        </blockquote></div>

                        <div class="tpc_content" id="read_11471225">来一个。<br>错题:__某仪仗队排成方阵,第一次排列若干人,结果多余10人,第二次比第一次每排增加3人,结果缺少29人,仪仗队总人数是多少?(&nbsp; &nbsp; )<br>&nbsp; A.4000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B.450&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C.500&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  D.600<br>正确答案:期待高手<br>做错的原因:我认为每一个答案都不对。</div>
<!--复制帖子加链接------>
一个文档内提取两关键词中间内容我知道在sed中是: sed -n "/pattern1/,/pattern2/p" test.dat>result 但是,如果是pattern1,和pattern2在文档中多处出现的话,提取不成功?或者提取的结果不对。麻烦各位老大看看。。。。新手学sed,先行谢过了:) ————————————————————————————————— 转移自 DOS疑难解答 & 问题讨论 (解答室) 明显发错版块,因属论坛新手,故暂不予以处罚,望今后一起注意。 ——管理员 ————————————————————————————————— [ Last edited by jesqiu on 2009-1-5 at 16:37 ]

作者: jesqiu     时间: 2008-12-26 16:37
感觉有点是因为<>这些符号处理有问题,还有就是sed的正则表达式的最大匹配和最小匹配。 试过提取<script language=JavaScript>......</script>,提取出来的结果是第一个<script...到最后一个</script>。所以,我很想搞明白的就是,多个匹配的内容,怎么精确匹配出来? 还有就是上面要提取的内容,<,=,-,!,>这些符号搞到我头都大了,有些加\,有些不加也可以。。。真晕了 再测试了下,貌似sed对中文支持不好:( script可以找到匹配的,"隐藏"就输出空了 [ Last edited by jesqiu on 2008-12-26 at 17:20 ]

作者: jesqiu     时间: 2008-12-26 18:29
天,怎么会发到DOS版。。。orz。。。头晕了 多谢管理员。。。不好意思。。。

作者: jesqiu     时间: 2008-12-29 11:32
有人愿意给点建议吗?想了几天确实没想到。。指点一二都好啊

作者: ily2013     时间: 2008-12-29 19:43
说实话你这个贴子发出来我就看了,不过贴的例子看得人眼都花了,今天我又看了看,因为我对sed也比较有兴趣,如果你的意思是只匹配第一次出现的pattern1/,/pattern2,可以用个笨办法,就是先删除pattern2,$在来一次pattern1/,/pattern2就解决问题了,你说的特殊字符加不加斜杠的问题,如果你不放心可以在前面加斜杠,其实pattern1/,/pattern2表达式是不需要把该行所有内容都写到表达式的,/隐藏2/,/复制帖子加链接/也可以的,具体代码我就不贴了,还是自己试验能学到东西!

作者: metoo     时间: 2008-12-29 21:21
这个需要用多行匹配模式,找点相关资料看看吧,算sed的中高级范畴了

作者: jesqiu     时间: 2008-12-31 18:30
Originally posted by ily2013 at 2008-12-29 19:43: 说实话你这个贴子发出来我就看了,不过贴的例子看得人眼都花了,今天我又看了看,因为我对sed也比较有兴趣,如果你的意思是只匹配第一次出现的 ...
其实第二楼就有简略写我的目的了 在发的时候就怀疑是不是人家看不明白了。本来想将数据作为附件上传的,不过论坛上传附件好像比较麻烦。试过了不太清楚。所以用code括起来 不是匹配第一次出现的。是匹配所有出现的模式。您说的pattern1/,/pattern2/不用写完整的,这个我知道,之前试过其他例子是可以的。但是这个数据不知道为什么不行?只是/隐藏2/这个不行(难道要用/隐藏/?不要数字2?这我还没测试过),如果用<script....> </script>这样的可以,就是得到最大匹配了(第一次出现<script...>到最后次出现</script>的所有内容,包括中间不需要的内容) 谢谢你:) [ Last edited by jesqiu on 2008-12-31 at 18:37 ]

作者: jesqiu     时间: 2008-12-31 18:32
Originally posted by metoo at 2008-12-29 21:21: 这个需要用多行匹配模式,找点相关资料看看吧,算sed的中高级范畴了
这位兄台说的没错,我知道要这个,但是网上好像找到的都是手册类的文档,没有这方面的啊。而且,我用的是windows下的sed版本,和xUnix下的又不太一样。好多都是自己摸索啊。需要高人指点其中的关键之处。。。。

作者: HAT     时间: 2009-1-1 01:13    标题: Re 8楼

本版有很多sed教程 批处理室教程收集专用帖www.cn-dos.net/forum ...

作者: jesqiu     时间: 2009-1-4 12:45
Originally posted by HAT at 2009-1-1 01:13: 本版有很多sed教程 批处理室教程收集专用帖www.cn-dos.net/forum ...
多谢多谢。我之前搜索标题带sed的文章,都看过了没发现有帮助的 多谢hat版主。先学习下先:)

作者: jesqiu     时间: 2009-1-4 18:59
彻底无语了。。。 几天前测试无数遍不可以的,今天一试居然得到正确结果了。。。太离奇了 sed -n "/隐藏/,/复制帖子加链接/p" Page1.rtf>xol.txt 无语。好像什么也不多什么也不少。。。

作者: jesqiu     时间: 2009-1-4 19:12
sed -n "/^<script language=javascript>/,/^<\/script>/!p" Page1.rtf>Page1.rtf 重定向到源文件的结果就是,空文件。。。orz。。。刚才发现。windows下的gnu sed 要重定向到不同名文件(或后缀)。 但是之前是可以的啊。。晕了,不知道怎么回事? [ Last edited by jesqiu on 2009-1-4 at 19:18 ]

作者: HAT     时间: 2009-1-4 20:02    标题: Re 12楼

使用 -i 开关即可直接修改原文件,不必重定向。

作者: jesqiu     时间: 2009-1-5 16:37
谢谢hat大人 问题已经解决