- 论坛徽章:
- 307
|
a File:
#ID Name Number
chr1 Armani 1027
chr2 Prada 7070
chr3 Chanel 2637
chr4 Dior 3487
chr5 NewBalance 4343 b File:
#ID Name Section1 section2
chr2 Prada 4000 5000
chr1 Armani 1019 1038
chr4 Dior 4716 4785
chr3 Chanel 3812 3942
codes:
- #!/usr/bin/perl
- use strict;
- use warnings;
- sub Load{
- local @ARGV = @_;
- my $bData = {};
- while(<>){
- next if(/\A#/);
- my ($id, $name, $sec1, $sec2) = split;
- $bData->{$id} = [$_, [$sec1, $sec2]];
- }
- $bData;
- }
- my $b = Load(pop);
- my %hFH = ();
- grep {open($hFH{$_}, '>', $_)} ('c', 'd');
- while(<>){
- next if(/\A#/);
- my ($id, $name, $num) = split;
- next if(!exists($b->{$id}));
- next if($b->{$id}[-1][0] > $num or $b->{$id}[-1][-1] < $num);
- select($hFH{'c'});
- print;
- select($hFH{'d'});
- print($b->{$id}[0]);
- }
- select(STDOUT);
- grep {close($hFH{$_})} keys %hFH;
复制代码
perl abc.pl a b
head -n 2 c d
-------------------------------
|
|