gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:01:41 25 4
gpt4 key购买 nike

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

$ awk 'length<=5' foo.txt

将只打印长度 <= 5 的行。

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.

关于Bash:如何通过插入连续字符和换行符来查找和拆分长行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20586145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com