Re: Converting comma-delimited data to tab-delimited - Mailing list pgsql-general

From merlyn@stonehenge.com (Randal L. Schwartz)
Subject Re: Converting comma-delimited data to tab-delimited
Date
Msg-id m1lmc84gho.fsf@halfdome.holdit.com
Whole thread Raw
In response to Converting comma-delimited data to tab-delimited  (Randall Perry <rgp@systame.com>)
Responses Re: Converting comma-delimited data to tab-delimited
List pgsql-general
>>>>> "Randall" == Randall Perry <rgp@systame.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@stonehenge.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!

pgsql-general by date:

Previous
From: Randall Perry
Date:
Subject: Re: Converting comma-delimited data to tab-delimited
Next
From: Randall Perry
Date:
Subject: Re: Converting comma-delimited data to tab-delimited