Bash: how to find and break up long lines by inserting continuation character and newline?

Viewed 2401

I know how to find long lines in a file, using awk or sed:

$ awk 'length<=5' foo.txt

will print only lines of length <= 5.

sed -i '/^.\{5,\}$/d' FILE

would delete all lines with more than 5 characters.

But how to find long lines and then break them up by inserting the continuation character ('&' in my case) and a newline?

Background:

I have some fortran code that is generated automatically. Unfortunately, some lines exceed the limit of 132 characters. I want to find them and break them up automatically. E.g., this:

 this is a might long line and should be broken up by inserting the continuation charater '&' and newline.

should become this:

 this is a might long line and should be broken &
 up by inserting the continuation charater '&' a&
 nd newline.
4 Answers
Related