Assuming your file is not millions of lines, try this

#!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

In reply to Re^5: Combined lines from a file into one by poj
in thread Combined lines from a file into one by emadmahou

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.