#!/usr/bin/perl -w # Title: tab2csv or csv2tab # Description: A program for converting between tab delimited files # and comma delimited files # Author: Philip Wilk # Copywrite: 2002 # History: # 31 July 2002 Birthday # # This script will operate as either program depending on what it is # called on the command line. I sym. link csv2tab to tab2csv and let # it figure out what I mean. # # This tool does not deal well with files that have bad quotes in them # (i.e. unmatched unescaped). It also probably does not work on files # with newlines in records. # # If you use this drop me an email: http://www.zenspider.com/Contact.html # It is most likely free for personal use, but you should email me. use Text::ParseWords; use strict; my $delim_in; my $delim_out; if ($0 =~ /tab2csv/) { $delim_in="\t"; $delim_out=","; }; if ($0 =~ /csv2tab/) { $delim_in=","; $delim_out="\t"; }; while (<>) { # read line into $_ my $row=""; chomp($_); my @cells = "ewords($delim_in, 0, $_); foreach (@cells) { $_ =~ s/\"/\\\"/; # escape any quotes that are supposed to be in there $row=$row."\"$_\"$delim_out"; } chop($row); # remove trailing delimitation print "$row\n"; }