1# ------------------------------------------------------------------------
2# Convert a CSV file into a TSV file
3# i.e. replace all delimiters from comma to TAB
4# All commas within strings are preserved
5# ------------------------------------------------------------------------
6
7my $f=@ARGV[0];
8open(IN,"<$f") or die "$f: $!";
9open(OUT,">$f.tsv") or die "$f.tsv: $!";
10
11my $line;
12while (defined ($line = <IN>)) {
13	chomp($line);
14	my @list = ($line =~ /(".*?"|[^,]*)(,)|(".*?"|[^,]*)/g) or die;
15	my $i=0;
16	while ($i <= $#list) {
17		if ($list[$i] eq ",") {$list[$i]="\t";}
18		$i++;
19	}
20	my $newline=join('',@list);
21	# Remove all double-quotes
22	$newline =~ s/\"//g;
23	print OUT "$newline\n";
24}
25
26close (IN);
27close (OUT);
28print "Created $f.tsv\n";
29