Make Multile line is one line using window Perl

Hi All

I have a .csv file which is showing data as

ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours 
(0)",0,"Approved Hours 
(112)",8,"Pending Hours 
(0)",0,"Pending and Approved Hours 
(112)",8,

ESP Client,ESP Engagement,Misc Projects_120101,DEFAULT,HR,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected - Pending,"she, aj v.",200111,Unknown,Full Time,,Delivery_Pending AMO,Approved,2012-12-04,2012-12-14,2012-12-14,"Occupied Hours 
(0)",0,"Approved Hours 
(112)",8,"Pending Hours 
(0)",0,"Pending and Approved Hours 
(112)",8,

I want to make in one line like using windows Perl

ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours (0)",0,"Approved Hours(112)",8,"Pending Hours(0)",0,"Pending and Approved Hours(112)",8,
ESP Client,ESP Engagement,Misc Projects_120101,DEFAULT,HR,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected - Pending,"she, aj v.",200111,Unknown,Full Time,,Delivery_Pending AMO,Approved,2012-12-04,2012-12-14,2012-12-14,"Occupied Hours(0)",0,"Approved Hours(112)",8,"Pending Hours(0)",0,"Pending and Approved Hours(112)",8,

What I have tried is

open('NEW',"<$new_file") || die "Error open file $new_file\n";

  while (my $s2=<NEW>) {
      if($s2 =~ /ESP General Client/){
          
          if ($flag==1){
              print NEW1 $new;
              $flag=0;
          }
          my $new=$s2;
      }
      else{
          $new .= ''.$s2;
          $flag=1;
      }
   }
    close(NEW);

But its not working

---------- Post updated at 09:01 AM ---------- Previous update was at 08:59 AM ----------

Sorry the actual code I am using is
open('NEW',"<$new_file") || die "Error open file $new_file\n"; while (my $s2=<NEW>) { if($s2 =~ /ESP Client/){ if ($flag==1){ print NEW1 $new; $flag=0; } my $new=$s2; } else{ $new .= ''.$s2; $flag=1; } } close(NEW);

---------- Post updated at 09:02 AM ---------- Previous update was at 09:01 AM ----------

Sorry the actual code i am using is

open('NEW',"<$new_file") || die "Error open file $new_file\n";

  while (my $s2=<NEW>) {
      if($s2 =~ /ESP Client/){
          
          if ($flag==1){
              print NEW1 $new;
              $flag=0;
          }
          my $new=$s2;
      }
      else{
          $new .= ''.$s2;
          $flag=1;
      }
   }
    close(NEW);

---------- Post updated at 12:24 PM ---------- Previous update was at 09:02 AM ----------

Its Make Multile line to one line using window Perl

The linefeeds are inside quotes, so the PERL CSV lib can pick them up, and all you need to do is replace line feed with space inside the fields.

Doing CSV in PERL without using the CSV libs seems like bad form, reinventing the wheel. If there were quoted commas in your fields, this would fall apart, so it is not robust.

Is it possible to do without CSV lib.
Everything is Possible in Perl

Possible, of course, but it smacks of intellectual laziness, lack of desire to learn or reuse. Perhaps API specs confuse him. Next! :smiley:

Can you please find the error, in above code. I think with some tweaking in the above code , it will start working.

perl -lne "$line=$line.' '.$_;if(/^$/ or eof){print $line;undef $line;}" your_file

Regards,
Vijay

Finally this thread ends up with this working code

my $new='';
$flat_file='flat_file.csv';
$new_file='new_file.csv';

open('NEW',"<$new_file") || die "Error open file $new_file\n";
open('NEW1',">$flat_file") || die "Error open file $flat_file\n";

  while (my $s2=<NEW>) {
       chomp($s2);
      if($s2 =~ /ESP Client/){
        print NEW1 "$new\n"; $new='';
                $new=$s2;
      }
      else{
          $new = $new.$s2;
      }
   }
    close(NEW);
    close(NEW1);

You can do it is 'sed', crudely, just loop running N until the buffer contains N commas, then substitute \n to a space globally. Of course, that's no good if quoted comma within any field, like most suggestions above. One easy way to detect N, say 19 commas is with s:

sed '
  :loop
  $b sub
  N
  s/,/,/19
  t sub
  b loop
  :sub
  s/\n/ /g
 ' in_files >out_file

If you know there are always 6 lines per row, just script in 5 N's. Then it is embedded comma safe (just not additional or less embedded linefeed safe).

Fixing each cell in PERL CSV library based storage is the robust solution. Robust is a $5 word. Learn the word for interviews, and the library for the interview test!