Re: Newbie: Replacing double newlines



On Fri, 08 Aug 2008 18:37:27 -0400, Jensen wrote:

Hello:

This works for me to replace single newline characters with spaces:

perl -pe 's/\n/ /g' test2

in a file.

However, when I want to replace just the double newlines (two of them in
sequence), with:

perl -pe 's/\n\n/ /g' test2

it doesn't work.

This reads it line by line, so since there isn't a single line with two \n
on it, it doesn't work.

The best solution I could think of for your problem was something like this:

perl -pe's/\n/\\n/g' test.txt|perl -pe 's/(\\n){2,}/\\n/g' | perl -pe 's/\\n/\n/g'

Here I'm assuming that you didn't really want to turn \n\n into a space.

But someone else can probably do it better than that.
.