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.
One way with sed
:
$ sed -r 's/.{47}/&\&\n/g' file
this is a might long line and should be broken &
up by inserting the continuation charater '&' a&
nd newline.
You can try:
awk '
BEGIN { p=47 }
{
while(length()> p) {
print substr($0,1,p) "&"
$0=substr($0,p+1)
}
print
}' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With