Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing newlines in Postgres dump

I'm trying to format a postgres dump (pg_dump) to be able to import it using a JDBC connection. pg_dump exports text fields that contain newlines to as just that, text with newlines, so when I later try to import using JDBC I reach the end of line and the statement fails.

What I want to do is take the dump, pass it through sed and escape all newlines, so that I end up with one INSERT statement per line. Problem is that I cannot just remove all newlines, but I can remove all newlines that do no match this );\nINSERT INTO. Is there a simple way to do just this?

Update: A sample would look like this:

INSERT INTO sometable (123, And here goes some text
with
newlines
in
it', 'some more fields');

and the result I'm looking for is something like this:

INSERT INTO sometable (123, And here goes some text\nwith\nnewlines\nin\nit', 'some more fields');

So that each INSERTstatement is on a single line, with the string's newlines escaped.

like image 600
cdecker Avatar asked Mar 24 '26 20:03

cdecker


1 Answers

Not a sed solution, but might the following work?

cat test_dump.txt | perl -pe "s/[^(\);INSERT INTO)]\n/\\$1\\n/"
like image 179
gsiems Avatar answered Mar 26 '26 15:03

gsiems



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!