On Ubuntu, how to reformat text to fit width (except final line), adding spaces where necessary? The closest I can get is with fmt --width=64
, but this does not add the spaces between words.
Input
- snippet taken from
man zip
with all line breaks removed and double spaces turned into single spaces
Do not operate on files modified prior to the specified date, where mm is the month (00-12), dd is the day of the month (01-31), and yyyy is the year. The ISO 8601 date format yyyy-mm-dd is also accepted. For example:
fold --width=64
output
- breaks words, which is undesirable
Do not operate on files modified prior to the specified date, wh
ere mm is the month (00-12), dd is the day of the month (01-31),
and yyyy is the year. The ISO 8601 date format yyyy-mm-dd is al
so accepted. For example:
fmt --width=65
output
- almost perfect, but also need to add spaces between words
Do not operate on files modified prior to the specified date,
where mm is the month (00-12), dd is the day of the month
(01-31), and yyyy is the year. The ISO 8601 date format
yyyy-mm-dd is also accepted. For example:
Wanted output
- snippet taken from
man zip
- where the double/triple spaces are inserted is not important to me, as long as the line fits the specified width and the words are more or less evenly-spaced
Do not operate on files modified prior to the specified date,
where mm is the month (00-12), dd is the day of the month
(01-31), and yyyy is the year. The ISO 8601 date format
yyyy-mm-dd is also accepted. For example:
RS=""
to read whole paragraphs at once. (b) Trim off the first 65 chars, and backtrack to a word gap. (c) Insert required n spaces by replacing one space in front of fields (NF - n + 1) to (NF). (d) Repeat {a..c}. (e) Avoid changing the last short line of a paragraph. – Paul_Pedant Mar 13 '24 at 09:04