-3

I need a help with the following problem. I have a file with the following data.

21997|||70049,,20170428154818,20170527235959|||
21997|||70070,,20170428154739,20170527235959|||

21998|||70049,,20170428154818,20170527235959|||
21998|||70070,,20170428154739,20170527235959|||
21998|||70071,,20170428154739,20170527235959|||

I need to unify the file as follows.

21997|||70049,,20170502172844,20170531235959; 70070,,20170502172844,20170531235959|||

21998|||70049,,20170502172844,20170531235959; 70070,,20170502172844,20170531235959; 70071,,20170502172844|||

Can someone help me please?

  • 3
    Hi and welcome to Stack Overflow! We prefer it if you show what you've tried rather than writing all the code for you. What have you tried? [You may wish to look at doing this with SQLite](http://stackoverflow.com/a/42563133/14660). – Schwern May 03 '17 at 23:20
  • 1
    How big is the file? Are the blocks always grouped by the value in the first column and separated by `\n\n`? – Matt Jacob May 04 '17 at 01:37

1 Answers1

-1
my $unified_output;
my %out;

open(FILE, "./raw-file.txt") or die $!;
    my @file = <FILE>;
close FILE;

for (@file) {       
    next if $_ =~ /$^/;         
    my @line = split(/\|\|\|/, $_) if $_;       
    $out{"$line[0]"} .= qq~$line[1]; ~ if $_ and $_ =~ /^$line[0]/;     
}

for (keys %out) {
    $out{$_} =~ s!\; $!!;
    $unified_output .= qq~$_|||$out{$_}|||\n~ if $_ and $out{$_};
}
bislinks
  • 84
  • 10
  • The only problem is that it should have generated one row for each record, but two lines were generated for one of the records. See below: 21998|||70049,,20170428154818,20170527235959; 70070,,20170428154739,20170527235959; 70071,,20170428154739,20170527235959||| 21998|||70049,,20170428154818,20170527235959; 70070,,20170428154739,20170527235959; 70071,,20170428154739,20170527235959||| 21998|||70049,,20170428154818,20170527235959; 70070,,20170428154739,20170527235959; 70071,,20170428154739,20170527235959||| 21997|||70049,,20170428154818,20170527235959; 70070,,20170428154739,20170527235959||| – Leonardo Berbert May 04 '17 at 00:42
  • It worked perfectly for me from what you gave: I got only two records/lines – bislinks May 04 '17 at 00:47
  • 1
    The Error was in my test mass with the ^ M in the content. Now it worked perfectly. Thanks for the help. – Leonardo Berbert May 04 '17 at 00:50
  • 1
    Please use modern best practices (lexical filehandles and three-arg `open()`) in example code. Also, you should consider reading the file a line at a time instead of reading it all into `@file`. – Dave Cross May 04 '17 at 07:58