in reply to replace \n with space to join indented lines

-p processes the file one line at a time, while both of your regexp rely on matching two lines at a time. There are other problems with your regexps, but that's the biggest. That can be solved by working with more than one line at a time.

my $file = do { local $/; <> }; 1 while $file =~ s/^([ ]{4}[^\n]+)\n[ ]{4}/$1/m; print($file);

Or better yet:

my $file = do { local $/; <> }; 1 while $file =~ s/^(([ ]+)(?![ ])[^\n]+)\n\2(?![ ])/$1/m; print($file);

Note: The second will unwrap all pragraphs, no matter how far they are indented.

Note: Both work with and without -i.

Note: 1 while s///; is used in order to repeatedly match the same line. s///g; won't work when the paragraph is more than two lines long.

Replies are listed 'Best First'.
Re^2: replace \n with space to join indented lines
by olivier (Initiate) on Dec 05, 2006 at 13:17 UTC
    Thank you ikegami. The problem is that I do have files that have paragraphs that contain more than 2 lines (sorry, I should have made that clearer in my original post). Is there a way in which you solution could be invoked as a one liner? Thank you

      If you want a one-liner, put it all on one line!

      perl -i.bak -e "local $/; $_=<>; 1 while s/^(([ ]+)(?![ ])[^\n]+)\n\2( +?![ ])/$1/m; print" myfile.txt