#!perl
use strict;
use warnings;
my $infile = 'rangeFile.txt';
my $outfile = 'test.txt';
open IN, '<', $infile or die "Cannot Open InFile: $infile : $!";
open OUT, '>', $outfile or die "Cannot Open OutFile: $outfile : $!";
my %out=();
my @key=();
my $count_in = 0;
my $count_out = 0;
# input
while (<IN>){
chomp;
++$count_in;
my @in = split ',',$_;
my $key = join ',',@in[0..2];
if ( ! defined $out{$key} ){
# initialise
@{$out{$key}} = @in[3..4];
push @key,$key; # preserve order
} else {
# min
if ($in[3] < $out{$key}[0]){
$out{$key}[0] = $in[3];
}
# max
if ($in[4] > $out{$key}[1]){
$out{$key}[1] = $in[4];
}
}
}
# output
for my $key (@key){
print OUT join ',',$key,@{$out{$key}},"\n";
++$count_out;
}
close IN;
close OUT;
print "
$count_in records read from $infile
$count_out records written to $outfile\n";
__DATA__
IMB,060410,V1 ,371094378,371096338,1961
IMB,060410,V1 ,371096340,371096486,147
IMB,107951,V1 ,981157588,981164939,7352
IMB,107951,V1 ,981164941,981165606,666
IMB,107951,V1 ,981165608,981175100,9493
IMB,107951,V1 ,981175102,981176199,1098
IMB,060410|,folded ,307057959,307058193,235
IMB,060410|,selfmail ,307058194,307066458,8265
IMB,107951|,folded ,958090350,958091491,1142
IMB,107951|,selfmail ,958091492,958132856,41365
SEQ,,folded ,000000001,000001377,1377
SEQ,,selfmail ,000001378,000051007,49630
poj |