Format output to a specific line length

awksedtext processingtext;

So I have some output that has very long lines, and I want to have it formatted to lines no more than 80 columns wide, but I don't want to split words because they are in the column limit.

I tried

sed 's/.\{80\}/&\n/g' 

but has the problem of splitting words and making some lines begin with a space.

I managed to do it with Vim, setting textwidth to 80, going to the beginnig of the file and executing gqG to format the text. But ยก would rather do it with sed, awk or something similar to include it in a script.

Best Answer

Use fmt instead:

fmt --width=80 file

From man fmt:

-w, --width=WIDTH
              maximum line width (default of 75 columns)
Related Question