Multiline string substitution on bash stream

This sed command, as the replacement of some_command, will delete all lines between a line beginning with COPY and a line consisting of \., including those two lines.

sed '/^COPY/,/^\\\.$/d'

First off, you need to escape the period as well \.. And since you have some beginning ^ and end of line $ restrictions, you should add those as well.

Since you are streaming the input, you can't undef $/ or it will try to read all 500Gb into memory, and your program will probably just hang. You have to read in line-by-line mode.

You might try a flip-flop operator:

perl -ne'print unless /^COPY/ ... /^\\\.$/' psql.txt

The range operator ... (or ..) will be true if the LHS pattern match is true, and for every line after, until and including when the RHS pattern is true. And false otherwise.

So we print all the lines that are not included in this passage.


Just use pg_dump with the --schema-only option,

--schema-only Dump only the object definitions (schema), not data. This option is the inverse of --data-only. It is similar to, but for historical reasons not identical to, specifying --section=pre-data --section=post-data.