Re: Converting comma-delimited data to tab-delimited

From: Randall Perry <rgp(at)systame(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Converting comma-delimited data to tab-delimited
Date: 2002-03-31 20:34:56
Message-ID: B8CCDD20.1243B%rgp@systame.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tried the following to replace multiple occurences of commas within quotes,
but the 2nd while loop never fires:

while (<>) {
while (m/\"(.*)(,)(.*)\"/) {
s/\"(.*)(,)(.*)\"/$1\x00$3/;
}
s/,/\t/g;
s/\x00/,/g;
print $_;
}

Can anyone help?

> Just discovered the following code only works if there's only ONE comma
> between quotes. Back to the drawing board :(
>
>> I answered my question by writing the following perl script:
>>
>>
>> #!/usr/local/bin/perl
>> # comma_to_tab.pl
>> #
>> # usage: ./comma_to_tab.pl data.csv > data.tab
>> #
>> # Description:
>> # 1. finds any occurences of a comma between double quotes and replaces
>> it with low ASCII char x00
>> # 2. replaces all remaining commas with tabs
>> # 3. changes all x00 chars back to commas
>>
>> while (<>) {
>> s/\"(.*)(,)(.*)\"/$1\x00$3/g;
>> s/,/\t/g;
>> s/\x00/,/g;
>> print $_;
>> }
>>
>>
>>> perl -ne 's/^ *"//; s/" *$//; print join("\t", split(/\" *, *\"/))'
>>> your-table.csv > your-table.tab

--
Randy Perry
sysTame
Mac Consulting/Sales

phn 561.589.6449
mobile email help(at)systame(dot)com

Browse pgsql-general by date

  From Date Subject
Next Message Randal L. Schwartz 2002-03-31 20:41:39 Re: Converting comma-delimited data to tab-delimited
Previous Message Randall Perry 2002-03-31 20:08:50 Re: Converting comma-delimited data to tab-delimited