Rewrapping Paragraphs
Problem
You have some text with lines that are too long or too short, so you’d like to re-wrap them to be more readable.
Solution
Use the fmt command, optionally with a goal and maximum line length:
$ fmt mangled_text $ fmt 55 60 mangled_text
Discussion
One tricky thing about fmt is that it expects blank lines to separate headers and paragraphs. If your input file doesn’t have those blanks, it has no way to tell the difference between different paragraphs and extra newlines inside the same paragraph. So you will end up with one giant paragraph, with the correct line lengths.
The pr command might also be of some interest for formatting text.
See Also
man fmt
man pr
Get bash Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.