Re: Converting comma-delimited data to tab-delimited

From: merlyn(at)stonehenge(dot)com (Randal L(dot) Schwartz)
To: Randall Perry <rgp(at)systame(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Converting comma-delimited data to tab-delimited
Date: 2002-03-31 20:41:39
Message-ID: m1lmc84gho.fsf@halfdome.holdit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Randall" == Randall Perry <rgp(at)systame(dot)com> writes:

Randall> Searched through the archives and found this perl one-liner that's supposed
Randall> to replace commas with tabs in text files.

Randall> It worked in as much as it created the new output file; but the output was
Randall> exactly the same as the input.

Randall> Anyone know what's wrong? Or have another way to do this?

Randall> perl -ne 's/^ *"//; s/" *$//; print join("\t", split(/\" *, *\"/))'
Randall> your-table.csv > your-table.tab

CSV is a bear, and best parsed with the Text::CSV module:

use Text::CSV;

my $t = Text::CSV->new;
while (<>) {
chomp;
csv->parse($_);
print join("\t", $csv->columns), "\n";
}

Which correctly handles quotish things, commas in quotes, etc.

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn(at)stonehenge(dot)com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Randall Perry 2002-03-31 21:25:12 Re: Converting comma-delimited data to tab-delimited
Previous Message Randall Perry 2002-03-31 20:34:56 Re: Converting comma-delimited data to tab-delimited