File: csv_2_tsv.pl

package info (click to toggle)
eccodes 2.44.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 150,256 kB
  • sloc: cpp: 163,056; ansic: 26,308; sh: 21,602; f90: 6,854; perl: 6,363; python: 5,087; java: 2,226; javascript: 1,427; yacc: 854; fortran: 543; lex: 359; makefile: 274; xml: 183; awk: 66
file content (28 lines) | stat: -rwxr-xr-x 736 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# ------------------------------------------------------------------------
# Convert a CSV file into a TSV file
# i.e. replace all delimiters from comma to TAB
# All commas within strings are preserved
# ------------------------------------------------------------------------

my $f=@ARGV[0];
open(IN,"<$f") or die "$f: $!";
open(OUT,">$f.tsv") or die "$f.tsv: $!";

my $line;
while (defined ($line = <IN>)) {
	chomp($line);
	my @list = ($line =~ /(".*?"|[^,]*)(,)|(".*?"|[^,]*)/g) or die;
	my $i=0;
	while ($i <= $#list) {
		if ($list[$i] eq ",") {$list[$i]="\t";}
		$i++;
	}
	my $newline=join('',@list);
	# Remove all double-quotes
	$newline =~ s/\"//g;
	print OUT "$newline\n";
}

close (IN);
close (OUT);
print "Created $f.tsv\n";