Skip to content

Instantly share code, notes, and snippets.

@nanasess
Created October 18, 2011 06:13
Show Gist options
  • Save nanasess/1294732 to your computer and use it in GitHub Desktop.
Save nanasess/1294732 to your computer and use it in GitHub Desktop.
csv2tsv
#!/usr/bin/perl
use strict;
use warnings;
my $column = '';
while (my $line = <STDIN>) {
my $count = 0;
$line .= <STDIN> while ($line =~ tr/"// % 2 and !eof(STDIN));
$line =~ s/(?:\x0D\x0A|[\x0D\x0A])?$/,/;
my @values = map {/^"(.*)"$/s ? scalar($_ = $1, s/""/"/g, $_) : $_}
($line =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g);
for $column (@values) {
$column =~ s/\x0D\x0A|\x0D|\x0A//g;
print $column, "\t";
}
print "\r\n";
}
__END__
@nanasess
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment