- 论坛徽章:
- 2
|
回复 39# jiujiujiu338 - #!/bin/perl
- use strict;
- open (my $fh1,"file1");
- open (my $fh2,"file2");
- open (my $fh3,">","file3");
- my %hash1;
- my %hash2;
- my %hash3;
- my %hash4;
- 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) {
- my $key1=$1 if($p=~/.*\nname=word(\d+).*/s);
- $hash4{$key1}.=$p;
- $hash3{substr($_,0,index($_," "))}=$_ foreach (split /\n/,$hash1{$p});
- $hash4{$key1}.=$hash3{$_}."\n" foreach sort {$a<=>$b} (keys %hash3);
- $hash4{$key1}.="end\n\n";
- undef(%hash3);
- }
- foreach (sort {$a<=>$b} (keys %hash4)) { print $fh3 $hash4{$_};}
-
-
- close($fh1);
- close($fh2);
- close($fh3);
复制代码 |
|