免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6552 | 回复: 11
打印 上一主题 下一主题

[文本处理] [正则表达]如何实现字典方法过滤?[已解答] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-09-09 18:15 |只看该作者 |倒序浏览
本帖最后由 peterdocter 于 2013-09-17 12:40 编辑

如何实现字典方法过滤,应该也叫循环方式过滤吧?
有两个文件,test1.txt是原内容记录,注意都一条过过。test2.txt就是要过滤条件字符。
只要test2.txt存在字符,就把些字符所在整条记录过滤掉或删除了。
本人尝试用shell+sed,1W要过滤字符。3天都没有处理完,kao。看来可能用awk比较快,但是又不懂awk如何编写?
  1. test1.txt
  2. 1080*#<h2>1080</h2>*#<a href="sound://10800001.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a><span class="main-fl"><em >noun</em></span>*#<span  class="pr">\(<span class="unicode">ˌ</span>)ten-<span class="unicode">ˈ</span>ā-tē\</span>*#<h2 class="def-header">*#<span>Definition:*#</span>*#</h2>*#<span class="ssens"><strong>:</strong>a poisonous preparation of sodium fluoroacetate used as a rodenticide and pesticide </span>*#<h2>*#<span>Variants:*#</span>*#</h2>*#<strong>1080</strong>*#<a href="sound://10800001.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a> also  <strong>ten–eighty</strong><span  class="pr">\(<span class="unicode">ˌ</span>)ten-<span class="unicode">ˈ</span>ā-tē\</span>*#<h2>*#<span>Origin:*#</span>*#</h2>*#from its laboratory serial numberFirst Known Use: 1945*#</>
  3. 1080s*#<h2>1080</h2>*#<a href="sound://10800001.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a><span class="main-fl"><em >noun</em></span>*#<span  class="pr">\(<span class="unicode">ˌ</span>)ten-<span class="unicode">ˈ</span>ā-tē\</span>*#<h2 class="def-header">*#<span>Definition:*#</span>*#</h2>*#<span class="ssens"><strong>:</strong>a poisonous preparation of sodium fluoroacetate used as a rodenticide and pesticide </span>*#<h2>*#<span>Variants:*#</span>*#</h2>*#<strong>1080</strong>*#<a href="sound://10800001.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a> also  <strong>ten–eighty</strong><span  class="pr">\(<span class="unicode">ˌ</span>)ten-<span class="unicode">ˈ</span>ā-tē\</span>*#<h2>*#<span>Origin:*#</span>*#</h2>*#from its laboratory serial numberFirst Known Use: 1945*#</>
  4. 12-step*#<h2>12–step</h2>*#<a href="sound://12ste01v.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a><span class="main-fl"><em >adj</em></span>*#<span  class="pr">\<span class="unicode">ˈ</span>twelv-<span class="unicode">ˌ</span>step\</span>*#<h2 class="def-header">*#<span>Definition:*#</span>*#</h2>*#<span class="ssens"><strong>:</strong>of, relating to, characteristic of, or being a program that is designed especially to help an individual overcome an addiction, compulsion, serious shortcoming, or traumatic experience by adherence to 12 tenets emphasizing personal growth and dependence on a higher spiritual being </span>*#<h2>*#<span>First Known Use:*#</span>*#</h2>*#1983*#</>
  5. 18-wheeler*#<h2><a href="entry://18–wheel.er">18–wheel.er</a></h2>*#<a href="sound://18_whe01.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a><span class="main-fl"><em >noun</em></span>*#<span  class="pr">\<span class="unicode">ˌ</span>ā(t)-(<span class="unicode">ˌ</span>)tēn-<span class="unicode">ˈ</span>wē-lər\</span>*#<h2 class="def-header">*#<span>Definition:*#</span>*#</h2>*#<span class="ssens"><strong>:</strong>a trucking rig consisting of a tractor and a trailer and typically having eighteen wheels </span>*#<h2>*#<span>Variants:*#</span>*#</h2>*#<strong><a href="entry://18–wheel.er">18–wheel.er</a></strong>*#<a href="sound://18_whe01.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a> or  <strong><a href="entry://eighteen–wheeler">eigh.teen–wheel.er</a></strong><span  class="pr">\<span class="unicode">ˌ</span>ā(t)-(<span class="unicode">ˌ</span>)tēn-<span class="unicode">ˈ</span>wē-lər\</span>*#<h2>*#<span>First Known Use:*#</span>*#</h2>*#1976*#</>
  6. 18-wheelers*#<h2><a href="entry://18–wheel.er">18–wheel.er</a></h2>*#<a href="sound://18_whe01.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a><span class="main-fl"><em >noun</em></span>*#<span  class="pr">\<span class="unicode">ˌ</span>ā(t)-(<span class="unicode">ˌ</span>)tēn-<span class="unicode">ˈ</span>wē-lər\</span>*#<h2 class="def-header">*#<span>Definition:*#</span>*#</h2>*#<span class="ssens"><strong>:</strong>a trucking rig consisting of a tractor and a trailer and typically having eighteen wheels </span>*#<h2>*#<span>Variants:*#</span>*#</h2>*#<strong><a href="entry://18–wheel.er">18–wheel.er</a></strong>*#<a href="sound://18_whe01.spx"><img align="absmiddle" border="0" src="/webster2012_audio.gif"></a> or  <strong><a href="entry://eighteen–wheeler">eigh.teen–wheel.er</a></strong><span  class="pr">\<span class="unicode">ˌ</span>ā(t)-(<span class="unicode">ˌ</span>)tēn-<span class="unicode">ˈ</span>wē-lər\</span>*#<h2>*#<span>First Known Use:*#</span>*#</h2>*#1976*#</>
复制代码
  1. test2.txt
  2. 12-step
  3. 18-wheelers
复制代码
如:
test2.txt
12-step 就把
test1.txt
12-step*#<h2>12–step</h2>*#...整条记录都过滤掉或删除。
注意:“配置条件是^12-step*#才进行过滤。”

发布内容格式都变了,请用附件中的demo吧。多谢了!
test.rar (910 Bytes, 下载次数: 8)

论坛徽章:
15
2015年辞旧岁徽章
日期:2015-03-03 16:54:15双鱼座
日期:2015-01-15 17:29:44午马
日期:2015-01-06 17:06:51子鼠
日期:2014-11-24 10:11:13寅虎
日期:2014-08-18 07:10:55酉鸡
日期:2014-04-02 12:24:51双子座
日期:2014-04-02 12:19:44天秤座
日期:2014-03-17 11:43:36亥猪
日期:2014-03-13 08:13:51未羊
日期:2014-03-11 12:42:03白羊座
日期:2013-11-20 10:15:18CU大牛徽章
日期:2013-04-17 11:48:45
2 [报告]
发表于 2013-09-09 19:14 |只看该作者
  1. awk -F "*#" 'NR==FNR{a[$1];next} {if ($1 in a) next}1' file2.txt file1.txt
复制代码

论坛徽章:
93
2015年辞旧岁徽章
日期:2019-10-10 10:51:15CU大牛徽章
日期:2014-02-21 14:21:56CU十二周年纪念徽章
日期:2020-10-15 16:55:55CU大牛徽章
日期:2014-02-21 14:22:07羊年新春福章
日期:2019-10-10 10:51:39CU大牛徽章
日期:2019-10-10 10:55:38季节之章:春
日期:2020-10-15 16:57:40ChinaUnix元老
日期:2019-10-10 10:54:42季节之章:冬
日期:2019-10-10 10:57:17CU大牛徽章
日期:2014-02-21 14:22:52CU大牛徽章
日期:2014-03-13 10:40:30CU大牛徽章
日期:2014-02-21 14:23:15
3 [报告]
发表于 2013-09-10 09:27 |只看该作者

不知道 grep -vf test2 test1 与 awk 的效率相比如何,楼主有实验结果望分享。

论坛徽章:
0
4 [报告]
发表于 2013-09-10 10:25 |只看该作者
回复 2# rdcwayx
多谢了!几分钟就可以处理10W多条记录,果然用awk效率比较好!

   

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
5 [报告]
发表于 2013-09-10 10:29 |只看该作者
回复 4# peterdocter
  1. grep -vFf test2 test1
复制代码
这个效率如何?

论坛徽章:
36
摩羯座
日期:2013-09-23 16:37:312015年亚洲杯之沙特阿拉伯
日期:2015-04-14 09:10:172015亚冠之柏太阳神
日期:2015-06-25 08:48:212015亚冠之武里南联
日期:2015-07-28 09:01:082015亚冠之莱赫维亚
日期:2015-07-28 15:44:172015亚冠之柏斯波利斯
日期:2015-09-06 14:08:52白银圣斗士
日期:2015-11-25 17:06:2815-16赛季CBA联赛之吉林
日期:2015-12-09 16:59:072016猴年福章徽章
日期:2016-02-18 15:30:3415-16赛季CBA联赛之辽宁
日期:2016-04-14 09:29:04luobin
日期:2016-06-17 17:46:3615-16赛季CBA联赛之天津
日期:2016-08-16 14:11:01
6 [报告]
发表于 2013-09-10 10:29 |只看该作者
seesea2517 发表于 2013-09-10 09:27
不知道 grep -vf test2 test1 与 awk 的效率相比如何,楼主有实验结果望分享。



我在我的机器上试了下,awk快!

论坛徽章:
0
7 [报告]
发表于 2013-09-10 10:36 |只看该作者
回复 3# seesea2517
这个有问题,如果有一条
18-wheelers....
也会过滤掉,我要的是18-wheelers*#才进行过滤。


   

论坛徽章:
0
8 [报告]
发表于 2013-09-10 10:39 |只看该作者
本帖最后由 peterdocter 于 2013-09-10 10:39 编辑

回复 5# Shell_HAT
同上面一样,把条件不匹配也过滤了


   

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
9 [报告]
发表于 2013-09-10 10:47 |只看该作者
回复 8# peterdocter
  1. grep -vxFf test2 test1
复制代码
  1. grep -vwFf test2 test1
复制代码

论坛徽章:
0
10 [报告]
发表于 2013-09-10 10:58 |只看该作者
回复 9# Shell_HAT
一样没有效果?

   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP