I'm working on migrating from MDaemon to Google Apps. As part of this process I'm exporting the contacts and calendars for all of our users into CSV files then reading them into a perl script and importing them into Google Apps.

Here's a sample calendar export shown in vi with ":set list" on.

"Event 1","05/05/2009","0:00:00","05
"Event 2","09/21/2009","11:00:00","09/21/2009","11:30:00","False","False",,,"email@TELUS.COM","",,"Notes from=A0^M$
"Event 3","04/05/2010","0:00:00","04/05/2010","23:59:00","True","True","04/04/2010","23:45:00",,,,"",,,"False","Normal",0$
"Event 4","05/05/2010","0:00:00","05/05/2010","23:59:00","True","True","05/04/2010","23:45:00",,,,"",,,"False","Normal",0$

I'm running into trouble with the lines ending in ^M$ which are supposed to be embedded line endings. My CSV parser Text::CSV_XS is parsing them as multiple lines rather than a single line. How can I replace the ^M$ with ^M? Any other suggestions are welcome.

Here's my parsing code:

 my $csv = Text::CSV_XS->new ({ binary => 1, eol => $/,auto_diag => 1 }) or
 die "Cannot use CSV: ".Text::CSV_XS->error_diag ();
 open my $fh, "<", $username . "-calendar.csv" or die $!;
 while (my $row = $csv->getline ($fh)) {
        print STDERR "NEW LINE: " . Dumper($row) . "END NEW LINE\n";
     push @rows, $row;
 $csv->eof or $csv->error_diag ();
 close $fh;

UPDATE 1: Here's the output from "file" regarding this file:

Non-ISO extended-ASCII English text, with very long lines, with CRLF, LF line terminators

  • 1
    If you want unix line endings, check out dos2unix. It's a pretty nifty tool. – VenkatH May 22 '12 at 18:38
  • I've tried running dos2unix on this file but it doesn't seem to affect the file. – dkwiebe May 22 '12 at 19:36
  • Give this a try? Change file format in Vim – VenkatH May 23 '12 at 16:47
  • That didn't do it either. I got it working with s/\r\r\n/<br>/g to replace the embedded line breaks with an html <br>. – dkwiebe May 25 '12 at 20:06

I got it working with s/\r\r\n/<br>/g to replace the embedded line breaks with an html <br>.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.