title: Perl to group columns in csv with multilines
body: <p>I have a csv (separated by ,) with multilines. The csv has 4 columns, of which first 3 columns contain multiline text while the group by happens on last column.</p>
  
  <p><strong>Input csv content</strong>: /tmp/test.tmp.csv</p>
  
  <pre><code>"Total Sections",ota,4!n,01
  "Input History",80,"HHMM28!c1!a[4!a]
  6X
  9X]",1
  "T (MR)",17t,(MTR),02
  "Input History",80,"HHMM28!c1!a[4!a]
  6X
  9X]",2
  Reference,:4!t/1c,:(Text1)/(Text2),30
  Reference,:4!t/1c,:(Text1)/(Text2),32
  </code></pre>
  
  <p>Above csv consists of 6 records, record 2 and 4 are with multilines.</p>
  
  <p><strong>Expected output</strong> (group by separate is space): </p>
  
  <pre><code>"Total Sections",ota,4!n,01
  "Input History",80,"HHMM28!c1!a[4!a]
  6X
  9X]",1 2
  "T (MR)",17t,(MTR),02
  Reference,:4!t/1c,:(Text1)/(Text2),30 32
  </code></pre>
  
  <p>My <strong>perl script</strong> (read first 3 fields in hash as key, the last field in hash as value, print to csv with join):</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use strict;
  use warnings;
  use Text::CSV;
  
  my %hash;
  my @array;
  my $in_qfn = "/tmp/test.tmp.csv";
  my $out_qfn = "/tmp/test.out.tmp.csv";
  
  # Li: Parsing multilines in csv
  my $parser = Text::CSV-&gt;new({
     binary =&gt; 1,
     auto_diag =&gt; 1,
     sep_char =&gt; ','
  });
  
  # Li: output multilines to csv
  my $csvo = Text::CSV-&gt;new({
     binary =&gt; 1,
     eol =&gt; "\r\n",
     sep_char =&gt; ','
  });
  
  open(my $data, '&lt;:encoding(utf8)', $in_qfn) or die "Could not open $in_qfn: $!\n";
  open(my $sts, '&gt;:encoding(utf8)', $out_qfn) or die "Could not write $out_qfn: $!\n";
  
  while (my $fields = $parser -&gt; getline($data)) {
     my $fz = $fields-&gt;[0];
     my $fo = $fields-&gt;[1];
     my $ft = $fields-&gt;[2];
     my $fth = $fields-&gt;[3];
     my @flds = ($fz, $fo, $ft, $fth);
  
     # Li: push the first 3 columns as key and the last column as value
     push(@{$hash{@flds[0..2]} }, $flds[3]);
  }
  
  # Li: print to output csv without join yet
  for my $k (sort keys %hash) {
     my @fldsAll = ($k, @{ $hash{$k}});
     print("###LI### 1: key: $k, value: @fldsAll\n");
     $csvo -&gt; print($sts, \@fldsAll);
  }
  </code></pre>
  
  <p>However, the script doesn't work perfectly, the hash key got lost due to the multiline and possibly special characters and without double quote everywhere. </p>
  
  <p><strong>Defected output</strong>:</p>
  
  <pre><code>(MTR),02
  4!n,01
  :(Text1)/(Text2),30,32
  "HHMM28!c1!a[4!a]
  6X
  9X]",1,2
  </code></pre>
  
  <p>Any idea on how to fix it? Or a brand new perl solution is also appreciate.</p>