Skip to content

Instantly share code, notes, and snippets.

@acg
Last active January 10, 2019 05:00
Show Gist options
  • Save acg/5312238 to your computer and use it in GitHub Desktop.
Save acg/5312238 to your computer and use it in GitHub Desktop.
Convert tsv to csv with optional unescaping.
#!/usr/bin/env perl
use Text::CSV;
use Getopt::Long qw/ GetOptionsFromArray :config pass_through /;
use warnings;
use strict;
my $usage = "usage: $0 [-e] < file.tsv\n";
exit main( @ARGV );
sub main
{
my $escape = 0;
GetOptionsFromArray( \@_, 'escape|e' => \$escape ) or die $usage;
my $csv = Text::CSV->new( { binary => 1 } );
my $out = \*STDOUT;
while (<STDIN>)
{
chomp;
do { print $/; next } if /^\s*$/;
my @row = split /\t/;
@row = map c_unescape($_) => @row if $escape;
$csv->combine( @row ) or die "CSV error: ".$csv->error_diag;
print $out $csv->string.$/;
}
return 0;
}
sub c_unescape
{
my $str = shift;
my %unescapes =
(
"\\\\" => "\\",
"\\n" => "\n",
"\\t" => "\t",
);
$str =~ s/(\\[\\nt])/ $unescapes{$1} /ge;
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment