 <p>Set Perl to paragraph mode, prepend <code>\</code> to newlines:</p> <pre><code>perl -p00le 's/\n/ \\\n/g' </code></pre> <p>Output:</p> <pre><code>aa bb \ ccc aa \ bb \ abc def \ gh </code></pre> <p>This takes advantage of some <a href="http://perldoc.perl.org/perlrun.html" rel="nofollow">Perl flags</a>:</p> <blockquote> <p><strong>-00</strong> The special value 00 will cause Perl to slurp files in paragraph mode.</p> <p><strong>-l</strong> enables automatic line-ending processing. It has two separate effects. First, it automatically chomps $/ (the input record separator) when used with -n or -p. Second, it assigns $\ (the output record separator) to have the value of octnum so that any print statements will have that separator added back on.</p> </blockquote> <p>Here is what Deparse makes of it:</p> <pre><code>perl -MO=Deparse -p00le 's/\n/ \\\n/g' BEGIN { $/ = ""; $\ = "\n\n"; } LINE: while (defined($_ = &lt;ARGV&gt;)) { chomp $_; s/\n/ \\\n/g; } continue { print $_; } </code></pre>