This page describes how to convert lines with line breaks (newline) to a single paragraph with TR and SED on FreeBSD.

Tested with CAT, TR and SED on FreeBSD 14.3 on 2026-02-12.

What is line breaks (newline)?

Some text files has line breaks, that might look good on fixed width screens, but can not be read nor edited as a paragraph in other editing software. The is caused by line breaks, which is also known as newline or the newline character. Historically, in UNIX, each line is a data record and a newline character is the record terminator.

4 lines with line breaks (newline) as input.

In this example, I will convert 4 lines to a paragraph.

$ cat input.txt
WOPR is a powerful mainframe for simulation.
You can also play tic-tac-toe or a nice game of chess.

You can find this weeks password in the desk drawer.

Each line ends with a newline character. This can be confirmed with CAT options for line numbering and non-printable characters.

$ cat -e -n input.txt
1 WOPR is a powerful mainframe for simulation.$
2 You can also play tic-tac-toe or a nice game of chess.$
3 $
4 You can find this weeks password in the desk drawer.$

Convert lines with line breaks (newline) to paragraph with TR and SED.

This one-liner command will convert line breaks to paragraph. It will also make sure, that the file ends with a newline, so it meets the POSIX standard definition of a text file.

$ cat input.txt | tr '\n' ' ' | sed 's/  */ /g; s/ $/\n/' > output.txt

CAT reads input.txt line by line. TR converts each newline to space. This includes blank lines. SED collapses multiple spaces and converts final space to a newline.

Alternative with SED alone.

A very kind SED specialist has sent me a solution, that uses SED alone. This works perfect as well. It is not as modular as the solution above, but if you prefer to use SED alone, it is indeed a clever solution.

$ sed -ne 'H; ${g;s/\n//;s/\n/ /g;s/  */ /g;p;}' < input.txt > output.txt

Paragraph with newline as output.

The result is one clean paragraph, that will scale to any screen width and can be used in other editing software.

$ cat output.txt
WOPR is a powerful mainframe for simulation. You can also play tic-tac-toe or a nice game of chess. You can find this weeks password in the desk drawer.

Confirm the newline with CAT.

$ cat -e -n output.txt 
1 WOPR is a powerful mainframe for simulation. You can also play tic-tac-toe or a nice game of chess. You can find this weeks password in the desk drawer.$

The default utility FOLD can be used to display the paragraph in any width.

$ cat output.txt | fold -s -w 50
WOPR is a powerful mainframe for simulation. You
can also play tic-tac-toe or a nice game of
chess. You can find this weeks password in the
desk drawer.