- 论坛徽章:
- 2
|
本帖最后由 yinyuemi 于 2011-08-18 14:18 编辑
回复 31# jiujiujiu338 - #!/bin/perl
- use strict;
- open (my $fh1,"file1");
- open (my $fh2,"file2");
- open (my $fh3,">","file3");
- my %hash1;
- my %hash2;
- my %hash3;
- my $t="";
- my $tmp="";
- while (<$fh1>){
- if($_!~/end/ and $_!~/^$/){
- if($_!~/^\d/){
- $tmp=$1 if(/(name=\S+)\s+.*/);
- {$t.=$_;}
- }
- else{$hash1{$t}.=$_ ;}
- }
- else{$hash2{$tmp}=$t if($_=~/end/);
- $t="";
- $tmp="";
- }
- }
- while (<$fh2>){
- if($_!~/end/ and $_!~/^$/){
- if($_!~/^\d/){
- $tmp=$1 if(/(name=\S+)\s+.*/);
- {$t.=$_;}
- }
- else{$hash1{$t}.=$_ ;}
- }
- else{
- if($_=~/end/){
- if(exists($hash2{$tmp})){$hash1{$hash2{$tmp}}.=$hash1{$t};
- delete $hash1{$t};}
- else{$hash2{$tmp}=$t;}
- }
- $t="";
- $tmp="";
- }
- }
- foreach my $p (keys %hash1) {
- print $fh3 $p;
- $hash3{substr($_,0,index($_,"\."))}=$_ foreach (split /\n/,$hash1{$p});
- print $fh3 $hash3{$_},"\n" foreach sort {$a<=>$b} (keys %hash3);
- print $fh3 "end\n\n";
- undef(%hash3);
- }
- close($fh1);
- close($fh2);
- close($fh3);
复制代码 |
|