Wednesday, April 15, 2009

How to convert a tab delimited file to a csv file?

This is a simple Perl script to convert a tab delimited file to a csv file.

!/usr/bin/perl -w

while(<>){
s/\t/","/g;
chomp;
print "\"" . $_ . "\"\n";
}

Place this code in a file and cat another file to it lie this:

> cat somefile.tab | perl tab2csv.pl > somefile.csv

You could also do this with perl from the command line like this:

cat somefile.tab | perl -p -e 's/\t/","/g' > somefile.csv

I put it in a file so I don't have to keep writing the line above.

Commands used in this post: cat, perl 

No comments: