- 论坛徽章:
- 145
|
回复 6# Eva326
如果文件2有重复: ... 最后结果如何去除呢?
$ cat a.txt
1
1.1
2
3
$ cat b.txt
1 a1 11
1 a1 11
1 a2 12
3 c1 31
3 c2 32
3 c3 33
4 d 41
$ perl gen_x_txt.pl a.txt b.txt
Create output file: 1.txt
Create output file: 3.txt
$ cat 1.txt
a1 11
a2 12
$ cat 3.txt
c1 31
c2 32
c3 33
$ cat gen_x_txt.pl
#! /usr/bin/perl
use strict;
use warnings;
sub message{
print <<EOF;
Usage : $0 ID_FILE DATA_FILE
Example: $0 a.txt b.txt
EOF
exit 1;
}
message() if @ARGV != 2;
my(%hIdx, %hTxt, %hCnt);
my $sCnt = 0;
my($sFidx, $sFdat) = @ARGV;
open(my $FHidx, "<", $sFidx) or die "cannot open $sFidx file\n";
open(my $FHdat, "<", $sFdat) or die "cannot open $sFdat file\n";
while(<$FHidx>){
s/^\s+|\s+$//g;
next if(m/^(#|$)/);
$hIdx{$_} = 1;
}
while(<$FHdat>){
s/^\s+|\s+$//g;
next if(m/^(#|$)/);
my($sKey, @aData) = split;
next if(! exists $hIdx{$sKey});
next if($hCnt{"$sKey @aData"}++);
openfile($sKey) if(! exists $hTxt{$sKey});
print {$hTxt{$sKey}} join(" ",@aData),"\n";
}
sub openfile{
my($sKey) = @_;
my $sFile = "$sKey.txt";
open(my $FHout, ">", $sFile) or die "cannot open $sFile file\n";
print "Create output file: $sFile\n";
$hTxt{$sKey} = $FHout;
}
|
|