Replace string with new line + string in a big file

terminalunix

I need to replace the string ),( with )\n,( in a 10GB file. The \n in this case obviously stands for a newline.
I've tried so with this command:

sed -i '' '/),(/ s//),\n(/g' bigfile.sql

However, that didn't really work as expected. It did replace the string, but it didn't insert a newline, but simply a "n" (so it just neglected the backslash and escaped it).
Any other takes on this, or where's my mistake in the above command?

Best Answer

Try

sed -i '' $'/),(/ s//),\\\n(/g' bigfile.sql
  • $'...' indicates that the string is a C-style string. This allows you to use the \\ and \n escapes, among others.
  • In place of your \n, we have a \\ and then a \n. This passes a literal backslash, and then a literal newline, to sed. This allows it to recognize that the newline is a part of the pattern, instead of terminating it.

The following also works:

sed -i '' '/),(/ s//),\
(/g' bigfile.sql