c - 脚本 : insert an additional #include after the last #include in many, 许多文件

标签 c regex bash sed

有那么一瞬间,我希望能够摆脱 seda\ 命令,但是 sed(无论如何在我手中)并不是真正喜欢保持状态(在每个 #include 之后插入)。

那么有没有办法用 sed 做到这一点?有没有一种聪明的方法来做到这一点?

如果可以的话,我会求助于编写一个常规的 Python/Ruby 脚本,但这似乎是某个人可能在某个时候遇到过的问题。

最佳答案

在应用 sed 之前,您可以使用命令 tac 逐行反转文件,然后让 sed 执行插入而不是追加。当然还要确保 sed 只执行一次插入。像这样:

tac file | sed '1,/#include/ {/#include/i\
#include whatever
}' | tac

这应该可以解决问题。

关于c - 脚本 : insert an additional #include after the last #include in many, 许多文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063725/

相关文章:

html - 帮助 sed regex : extract text from specific tag

PHP正则表达式查找以_开头的图像

linux - Bash:在后台运行一个可执行文件

django - 如何将 'yes' 传递给 python manage.py flush?

c++ - 分析用 C 或 C++ 编写的程序

c++ - 我是否必须在 Xcode iOS 项目中释放 C 分配的内存?

iphone - 从另一个线程C/C++在主线程上执行类方法或函数(静态方法)

c - JNI : create an jobjectArray without knowing the size in advance

regex - 在 VS 中,如何使用正则表达式查找/替换但增加数值

bash - 成功启动 Elasticsearch 后如何执行操作