免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: jiujiujiu338
打印 上一主题 下一主题

帮忙合并两个文件中的不同内容 [复制链接]

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
1 [报告]
发表于 2011-08-17 11:34 |显示全部楼层
本帖最后由 yinyuemi 于 2011-08-17 11:38 编辑

回复 1# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash;
  7. my $t="";

  8. while (<$fh1>){
  9.         if($_!~/end/ and $_!~/^$/){
  10.                 if($_!~/^\d/){$t.=$_;}
  11.                 else{$hash{$t}.=$_ ;}
  12.                 }
  13.         else{$t="";
  14.                 }
  15. }
  16. while (<$fh2>){
  17.         if($_!~/end/ and $_!~/^$/){
  18.                 if($_!~/^\d/){$t.=$_;}
  19.                 else{$hash{$t}.=$_ ;}
  20.                 }
  21.         else{$t="";
  22.                 }
  23.         }

  24. foreach my $p (keys %hash) {
  25.         print  $fh3 $p, join "\n", (sort {$a<=>$b} (split /\n/, $hash{$p})),"end\n\n";
  26.         }

  27. close($fh1);
  28. close($fh2);
  29. close($fh3);
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
2 [报告]
发表于 2011-08-18 09:06 |显示全部楼层
回复 22# jiujiujiu338


   假如我那段perl程序名叫s.pl,运行下面的代码(如果你是win,把单引号改成双引号),再执行试试

    perl -i -lpne 's/end/word0/g' s.pl

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
3 [报告]
发表于 2011-08-18 11:36 |显示全部楼层
回复 25# jiujiujiu338

  1.     #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash;
  7. my $t="";

  8. while (<$fh1>){
  9.         if($_!~/word0/ and $_!~/^$/){
  10.                 if($_!~/^\d/){$t.=$_;}
  11.                 else{$hash{$t}.=$_ ;}
  12.                 }
  13.         else{$t="";
  14.                 }
  15. }
  16. while (<$fh2>){
  17.         if($_!~/word0/ and $_!~/^$/){
  18.                 if($_!~/^\d/){$t.=$_;}
  19.                 else{$hash{$t}.=$_ ;}
  20.                 }
  21.         else{$t="";
  22.                 }
  23.         }
  24. my %hash2;
  25. foreach my $p (keys %hash) {
  26.         print $fh3 $p;
  27.         $hash2{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash{$p});
  28.         print $fh3 $hash2{$_},"\n" foreach sort {$a<=>$b} (keys %hash2);
  29.         print $fh3 "word0\n\n";
  30.         undef(%hash2);
  31.         }

  32. close($fh1);
  33. close($fh2);
  34. close($fh3);
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
4 [报告]
发表于 2011-08-18 13:19 |显示全部楼层
回复 29# jiujiujiu338

  1. split /\n/ $hash{$p} # 把$hash{$p}的值,split 成一个数组,分隔符是'\n'
  2. foreach 函数读取上面的数组值,默认下用$_
  3. substr函数截取,比如“13.081 11.386”字符串中的13
  4. $hash2{}=$_ #建一个hash,key是 13,value是 “13.081 11.386”

复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
5 [报告]
发表于 2011-08-18 14:13 |显示全部楼层
本帖最后由 yinyuemi 于 2011-08-18 14:18 编辑

回复 31# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash1;
  7. my %hash2;
  8. my %hash3;
  9. my $t="";
  10. my $tmp="";

  11. while (<$fh1>){
  12.         if($_!~/end/ and $_!~/^$/){
  13.                 if($_!~/^\d/){
  14.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  15.                         {$t.=$_;}
  16.                 }
  17.                 else{$hash1{$t}.=$_ ;}
  18.                 }
  19.         else{$hash2{$tmp}=$t if($_=~/end/);
  20.                 $t="";
  21.                 $tmp="";
  22.                 }
  23. }
  24. while (<$fh2>){
  25.         if($_!~/end/ and $_!~/^$/){
  26.                 if($_!~/^\d/){
  27.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  28.                         {$t.=$_;}
  29.                 }
  30.                 else{$hash1{$t}.=$_ ;}
  31.                 }
  32.         else{
  33.                 if($_=~/end/){
  34.                         if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};
  35.                                 delete $hash1{$t};}
  36.                         else{$hash2{$tmp}=$t;}
  37.                 }
  38.                 $t="";
  39.                 $tmp="";
  40.                 }
  41.         }

  42. foreach my $p (keys %hash1) {
  43.         print $fh3 $p;
  44.         $hash3{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash1{$p});
  45.         print $fh3 $hash3{$_},"\n" foreach sort {$a<=>$b} (keys %hash3);
  46.         print $fh3 "end\n\n";
  47.         undef(%hash3);
  48.         }

  49. close($fh1);
  50. close($fh2);
  51. close($fh3);
复制代码

论坛徽章:
2
射手座
日期:2014-10-10 15:59:4715-16赛季CBA联赛之上海
日期:2016-03-03 10:27:14
6 [报告]
发表于 2011-08-19 13:36 |显示全部楼层
回复 39# jiujiujiu338
  1. #!/bin/perl
  2. use strict;

  3. open (my $fh1,"file1");
  4. open (my $fh2,"file2");
  5. open (my $fh3,">","file3");

  6. my %hash1;
  7. my %hash2;
  8. my %hash3;
  9. my %hash4;
  10. my $t="";
  11. my $tmp="";

  12. while (<$fh1>){
  13.         if($_!~/end/ and $_!~/^$/){
  14.                 if($_!~/^\d/){
  15.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  16.                         {$t.=$_;}
  17.                 }
  18.                 else{$hash1{$t}.=$_ ;}
  19.                 }
  20.         else{$hash2{$tmp}=$t if($_=~/end/);
  21.                 $t="";
  22.                 $tmp="";
  23.                 }
  24. }
  25. while (<$fh2>){
  26.         if($_!~/end/ and $_!~/^$/){
  27.                 if($_!~/^\d/){
  28.                         $tmp=$1 if(/(name=\S+)\s+.*/);
  29.                         {$t.=$_;}
  30.                 }
  31.                 else{$hash1{$t}.=$_ ;}
  32.                 }
  33.         else{
  34.                 if($_=~/end/){
  35.                         if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};delete $hash1{$t};}
  36.                         else{$hash2{$tmp}=$t;}
  37.                 }
  38.                 $t="";
  39.                 $tmp="";
  40.                 }
  41.         }

  42. foreach my $p (keys %hash1) {
  43.         my $key1=$1 if($p=~/.*\nname=word(\d+).*/s);
  44.         $hash4{$key1}.=$p;
  45.         $hash3{substr($_,0,index($_," "))}=$_ foreach (split /\n/,$hash1{$p});
  46.         $hash4{$key1}.=$hash3{$_}."\n" foreach sort {$a<=>$b} (keys %hash3);
  47.         $hash4{$key1}.="end\n\n";
  48.         undef(%hash3);
  49.         }

  50. foreach (sort {$a<=>$b} (keys %hash4)) { print $fh3 $hash4{$_};}
  51.        
  52.        
  53. close($fh1);
  54. close($fh2);
  55. close($fh3);
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP