Bash:如何通过插入连续字符和换行符来查找和分解长行?

2023-12-25

我知道如何使用 awk 或 sed 在文件中查找长行:

$ awk 'length<=5' foo.txt

将仅打印长度

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

将删除所有超过 5 个字符的行。

但是如何找到长行,然后通过插入连续字符(在我的例子中为“&”)和换行符来将它们分解?

背景:

我有一些自动生成的 Fortran 代码。不幸的是,有些行超出了 132 个字符的限制。我想找到他们并自动将他们分解。例如,这个:

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

应该变成这样:

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

一种方式是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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Bash:如何通过插入连续字符和换行符来查找和分解长行? 的相关文章

随机推荐