[lug] Those Pesky Newlines

Rob Nagler nagler at bivio.biz
Thu Dec 26 17:29:01 MST 2002


Peter Hutnick writes:
> Is there an obvious way to strip "line wrap" newlines while leaving
> "paragraph separating" newlines intact?  IOW, I need to strip all the
> newlines out of a file *except* ones on their own line and ones that are
> on a line with text that is followed by a line that consists only of a
> newline.

Try:

perl -pe 's/^$/\n/ || s/\n//' in > out

This will not print out a trailing newline for the last line in the
file.  If you need that, use

perl -pe 's/^$/\n/ || s/\n//; END {print "\n"}' in > out


Rob





More information about the LUG mailing list