I want to convert a tab-separated file into a CSV file. Can anyone help me?
I want to convert a tab-separated file into a CSV file. Can anyone help me?
Bear in mind that there are many flavours of comma-separated-value file. Since you didn't specify one, I'll assume RFC-4180 format, in UTF-8 encoding, and the TSV to be the same but using tabs instead of commas.
The naive approach would be to simply replace every tab with a comma:
tr '\t' ,
This falls down if any of the values already contain a comma, or if any contain a quoted tab. You'll need to minimally parse the file, to maintain quoting. Instead of hand-rolling such a parser, it's simpler, clearer and more flexible to use one already written, such as Text::CSV for Perl:
#!/usr/bin/perl -w
use Text::CSV;
my $tsv = Text::CSV->new({ sep_char => "\t", auto_diag => 2 });
my $csv = Text::CSV->new();
while (my $row = $tsv->getline(*ARGV)) {
$csv->print(STDOUT, $row) or die $csv->error_diag();
print $/;
}
$csv->error_diag() unless $tsv->eof;
If the input_file did not contain commas nor quoted tabs, the easiest method is to use the sed command with a substitution regular expression that replaces tabs in the input_file with commas:
sed 's/\t/,/g' input_file > output_file
There is of course the case of input_files containing quoted tabs that will confuse the regex, also input_files that has values containing commas, these values should be quoted in output_files. Thee regex solution above would not be sufficient to address those cases.
One could think of several bash commands to "clean" the input file before processing it...
However, in the spirit of not reinventing the wheel. I would use a simple python3 script and store it in a file, let's call it: tsv_to_csv_converter.py:
import csv
import sys
tsv_file = sys.argv[1]
csv_file = sys.argv[2]
in_tsv = csv.reader(open(tsv_file, "r"), delimiter = '\t')
out_csv = csv.writer(open(csv_file, 'w'))
out_csv.writerows(in_tsv)
Of course, this code can be dramatically improved with exception catching, input value checks, a nice CLI interface, documentation.
The script can be used as follows:
python3 tsv_to_csv_converter.py input_file output_file
After testing in MacOS, this is working for converting csv file to tsv file (suppose no tab or comma exists in the column values):
cat file_input.tsv | tr '\t' ',' > file_output.csv
I tried:
sed 's/ /,/g' input_file > output_file
and
sed 's/\t/,/g' input_file > output_file
However, neither of them worked.