Re: Converting comma-delimited data to tab-delimited
От | merlyn@stonehenge.com (Randal L. Schwartz) |
---|---|
Тема | Re: Converting comma-delimited data to tab-delimited |
Дата | |
Msg-id | m1lmc84gho.fsf@halfdome.holdit.com обсуждение исходный текст |
Ответ на | Converting comma-delimited data to tab-delimited (Randall Perry <rgp@systame.com>) |
Ответы |
Re: Converting comma-delimited data to tab-delimited
|
Список | 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 по дате отправления: