免费注册 查看新帖 |

Chinaunix

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

删除文本行的问题(如何实现匹配每个Reg,就删除) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-14 19:41 |只看该作者 |倒序浏览
参考了http://bbs.chinaunix.net/viewthr ... =%C9%BE%B3%FD%D0%D0,但目前有点晕

但是如果
txt1    这么三行
111
222
333

txt2

ai1114
959950
222435
jjll9442
44333
79777

如果要用txt1作为条件来匹配,只要txt2匹配上txt1任一条件的行就删除。  perl怎么实现? 循环有点晕了。
我的要求也就是显示不包含txt1任一条件的txt2中的行

[ 本帖最后由 mantou 于 2007-11-14 19:48 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-11-15 00:20 |只看该作者
#多想想

open(TXT1, '<', 'txt1.txt') || die "$!\n";
open(TXT2, '<', 'txt2.txt') || die "$!\n";

my @rules = <TXT1>;
chomp @rules;
my @results = ();

while (my $line = <TXT2>) {
    if (0 == map {$line =~ /\Q$_\E/} @rules) {
        push @results, $line;
    }
}

print @results;


[ 本帖最后由 Lonki 于 2007-11-15 00:22 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2007-11-15 09:37 |只看该作者
非常感谢您的帮助

这是参考的另外一种实现方法。我原来写的脚本问题出现在循环那里。
下面这种方法也可以提供给有我同样需求的人

#!/usr/bin/perl

open(F1,"oldnewid");
open(F2,"tt");
open(F3,">txt3.txt");
@a = <F2>;;
close(F2);
@b=<F1>;;
close(F1);

foreach $b1 (@b){

#chomp($b1);

&nbsp;&nbsp;foreach $a1 (@a)
&nbsp;&nbsp;{
&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;chomp($a1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($b1 =~ /$a1/){
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$flag =0;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;last;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$flag =1;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
&nbsp;&nbsp;if($flag ==1)
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print F3 "$b1";
&nbsp;&nbsp;}
}


[ 本帖最后由 mantou 于 2007-11-15 09:38 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-11-15 10:14 |只看该作者
原帖由 Lonki 于 2007-11-15 00:20 发表
#多想想

open(TXT1, '

Lonki兄最近也喜欢加上/\Q...\E/了啊,呵呵
最后用数组存储代替文件读写,是个小计巧啊,学习了

论坛徽章:
0
5 [报告]
发表于 2007-11-15 10:54 |只看该作者
Could you tell me what's the meaning \Q , \E although I know the following has the same function?  where can I find docs for it?  
  
if(0 == grep {$line =~ !/\Q$_\E/} @a)

Thanks!

论坛徽章:
0
6 [报告]
发表于 2007-11-15 12:32 |只看该作者

回复 #5 biginner 的帖子

你去正则表达式那一部分就能找到了,简单说就是 不转义
中间包含部分被看作纯字符串

论坛徽章:
0
7 [报告]
发表于 2007-11-15 12:52 |只看该作者

回复 #6 perljoker 的帖子

谢谢! 如此粗心

论坛徽章:
0
8 [报告]
发表于 2007-11-15 19:48 |只看该作者
原帖由 perljoker 于 2007-11-15 10:14 发表

Lonki兄最近也喜欢加上/\Q...\E/了啊,呵呵
最后用数组存储代替文件读写,是个小计巧啊,学习了



哎, 白天在公司只能看帖, 无法回复啊....


用\Q\E完全是情非得已, index会让代码增加好几行, 普通情况下还是建议用index

论坛徽章:
0
9 [报告]
发表于 2007-11-15 19:52 |只看该作者
原帖由 biginner 于 2007-11-15 10:54 发表
Could you tell me what's the meaning \Q , \E although I know the following has the same function?  where can I find docs for it?  
  
if(0 == grep {$line =~ !/\Q$_\E/} @a)

Thanks!



上面perljoker已经解释了\Q\E

你的这个if, 和LZ那一段并不是相同的作用.
grep是用同一条规则, 作于一个数组, 返回成功与否, 或匹配到的数组
而上面需要的是用一组规则, 作用于一个数组的每一个变量.

论坛徽章:
0
10 [报告]
发表于 2007-11-16 09:28 |只看该作者

回复 #9 Lonki 的帖子

Thanks for your patience!  

I took a mistake for I got the same result with " if(0 == grep {$line =~ !/\Q$_\E/} @a)  "
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP