emadmahou has asked for the wisdom of the Perl Monks concerning the following question:

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

I have a script that create these lines for me from an input file my problem is in the first line the range is from 371094378,371096338

In the second line the range start after two numbers 371096340

I am trying to get these output

IMB,060410,V1 ,371094378,371096486

This one is the combined of the first two line with the min value and the max value

IMB,107951,V1 ,981157588,981176199,1098

this one is the combined of the other value with min value and max The unique identifier is "060410" and "107951" any help please?

Replies are listed 'Best First'.
Re: Combined lines from a file into one
by GrandFather (Saint) on Aug 04, 2015 at 20:47 UTC
      My script works fine, but I am using a tool that create these ranges and that tool has a bug that it does skip a number or two some times that's why I have this issue of multiple line instead of one line with the whole range. Now I am trying to go throw the line find the min value and the max value and output them in one line in new file.
Re: Combined lines from a file into one
by Anonymous Monk on Aug 04, 2015 at 22:50 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1137428 use strict; use warnings; $_ = <<END; 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 END s/^(IMB,\d+,V1\s,)(\d+),\K (?:.*\n)+ \1\d+,(\d+).*/$3/gmx; print;
      Can you please explain to me what is happening in this line $_ = <<END; and instead of having the data in there can I read them from a file like
      use strict; use warnings; open (rangeOutput, '>', "test.txt") or die "Cannot Open File: test.txt +: $!"; open (rangeFixed, '<', "rangeFile.txt") or die "Cannot Open File: rang +eFile.txt $!"; while ( <rangeFixed> ) { $_ = s/^(IMB,\d+,V1\s,)(\d+),\K(?:.*\n)+\1\d+,(\d+).*/$3/gmx; print rangeOutput $_ . "\n"; }
      It didn't work for me when I tried to read the range from a file any suggestion ?
        open (ImbRangeHelper, '>', "test.txt") opens a file for writing , did you mean
        open (ImbRangeHelper, '<', "test.txt")
        poj
        #!/usr/bin/perl # http://perlmonks.org/?node_id=1137428 use strict; use warnings; open (my $rangeFixed, '<', "rangeFile.txt") or die "Cannot Open File: +rangeFile.txt $!"; $_ = join '', <$rangeFixed>; s/^(IMB,\d+,V1\s,)(\d+),\K (?:.*\n)+ \1\d+,(\d+).*/$3/gmx; open (my $rangeOutput, '>', "test.txt") or die "Cannot Open File: test +.txt: $!"; print $rangeOutput $_; close $rangeOutput;
Re: Combined lines from a file into one
by poj (Abbot) on Aug 05, 2015 at 08:05 UTC

    Do you want to include in the output the count (last column) on the new records ?

    If so, should it be the difference between the min and max ?. The 1098 shown on the second record no longer relates to the interval 981157588 to 981176199.

    poj
      No I don't need the count. I need to combine each line with the same unique number "060410" or "107951" after finding the max and min
      No I don't need the count. I need to combine each line with the same unique number "060410" or "107951" after finding the max and min. The count is part of the script I iused to find these ranges.
A reply falls below the community's threshold of quality. You may see it by logging in.