Quick tip: converting lines of text to paragraphs (and the reverse)

By Tom Moertel
Posted on
Tags: emacs, perl, text

Text documents come in two basic flavors: text editor and word processor. In the text-editor flavor, a document is represented by lines of text, each ending with a line break. Paragraphs are separated by two breaks. In the word-processor flavor, there are no line breaks, only paragraphs (because word processors will “wrap” lines of text as you edit them).

If you need to convert from text-editor flavor (lines) to word-processor flavor (paragraphs), here’s a handy Perl one-liner that will do it:

perl -lp00e's/\n/ /g' input.txt > output.txt

If you’re editing in Emacs, you can convert a selected region of text using the same one-liner via shell-command-on-region:

C-u M-| perl -lp00e's/\n/ /g' RET

To go the other way, from word-processor flavor to text-editor flavor, the Unix command-line tools fold(1) and fmt(1) do the job.

comments powered by Disqus