I'm running PSQL exports to CSV files for a few tables. They look like the below:
COPY table_name TO 'file_name' CSV
The issue is that some of these tables have text fields, in which the values for these fields contain both the delimiter (commas) and newlines. What would be the best way to do the export while removing the newlines across all columns?
Example table:
field1,field2,field3,field4
field1,field2,"field3, with, the delimiter",field4
field1,field2,"field3, with, the
delimiter and newline",field4
field1,"field2 with a
newline",field3,field4
How I'd want my export to look:
field1,field2,field3,field4
field1,field2,"field3, with, the delimiter",field4
field1,field2,"field3, with, the delimiter and newline",field4
field1,"field2 with a newline",field3,field4
Some solutions I've been considering:
- Write a custom regex replace function and update the tables before I do the export.
- See if there is a way to do the replace during the export transaction (is this possible?).
- Perform the export as is and use another library/language to post-process the exported CSV.
Thanks for the help!