Out of memory while using sed with multiline expressions on giant file

view story

http://unix.stackexchange.com – I am currently trying to remove all newlines that are not preceded by a closing parenthesis, so I came up with this expression: sed -r -i -e ":a;N;$!ba;s/([^\)])\n/\1/g;d" reallyBigFile.log It does the job on smaller files, but on this large file I am using (3GB), it works for a while then returns with an out of memory error: sed: Couldn't re-allocate memory Is there any way I could do this job without running into this issue. Using sed itself is not mandatory, I just want to get it done. (HowTos)