Both fmt
and par
are good tools for re-formatting text.
Both of them can be used to reformat your text into a single long line after the linefeeds have been replaced with spaces (e.g. with tr
):
$ tr '\n' ' ' < input.txt | fmt -w 999
I cannot conceive that anybody will require multiplications at the rate of 40,000 or even 4,000 per hour ... -- F. H. Wales (1936)
$ tr '\n' ' ' < input.txt | par w999
I cannot conceive that anybody will require multiplications at the rate of 40,000 or even 4,000 per hour ... -- F. H. Wales (1936)
Note that the output of fmt and par is slightly different. fmt has four spaces between the ...
and the --
, while par reduces them to one space.
More on fmt
and par
:
fmt
is a standard utility that has been around for decades, and should be available on almost any unix system. On GNU/Linux systems, it's in the GNU coreutils
package.
$ fmt < input.txt
I cannot conceive that anybody will require multiplications at the rate
of 40,000 or even 4,000 per hour ...
-- F. H. Wales (1936)
It is, however, very simple and does not allow much control over how the paragraphs can be reformatted, nor does it perform any special handling of leading and/or trailing characters (like >
quoting in email or /* ... */
comments), which can lead to a jumbled mess.
par
is much more flexible and capable. It can reformat C style /* ... */
comments and boxed text, email messages with multiple levels of quoting, and more. The following example doesn't show what it's really capable of.
$ par < input.txt
I cannot conceive that anybody will require multiplications at the rate of
40,000 or even 4,000 per hour ...
-- F. H. Wales (1936)
The following example (from man par
) begins to show par
's capabilities:
Before:
John writes:
: Mary writes:
: + Anastasia writes:
: + > Hi all!
: + Hi Ana!
: Hi Ana & Mary!
Please unsubscribe me from alt.hello.
After "par Q+:+ q":
John writes:
: Mary writes:
:
: + Anastasia writes:
: +
: + > Hi all!
: +
: + Hi Ana!
:
: Hi Ana & Mary!
Please unsubscribe me from alt.hello.
I've been using it daily from within vi/vim since I discovered the 1990s to reformat emails that I'm writing (or emails from others with unreadable long lines), code comments and other text. IMO, par is an indispensible, must-have program.