How to convert a tab-separated file into a comma-separated file? How to convert a tab-separated file into a comma-separated file? unix unix

How to convert a tab-separated file into a comma-separated file?


The answer for OSX is different.

MacOS does not understand \t in the sed expression.

You have to insert the tab literal into the sed search pattern by using ctrl+v then tab (see How can I insert a tab character with sed on OS X?)

sed 's/ /,/g' input_file > output_file


You can use sed as:

sed 's/\t/,/g' input_file > output_file

This will keep the input file unchanged and will create a new file output_file with the changes.

If you want to change the input file itself without creating a new file you can use -i option to sed to do inplace changes:

sed -i 's/\t/,/g' input_file 


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 -wuse 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;