unix - sed/awk : Insert some text in a file after the last pattern found

标签 unix sed awk

我要插入文字

#include < abc/malloc.h>

在所有的 C 文件中

#include

行,就在最后一次出现 #include 之后。

最佳答案

使用awk:

awk '
    FNR==NR && /^#include/ { line=NR; next }
    FNR!=NR
    FNR==line { print "#include <abc/malloc.h>" }
' code.c code.c

如你所见:

  • awk 阅读code.c两次
  • 第一次如果 FNR==NR

关于unix - sed/awk : Insert some text in a file after the last pattern found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22380843/

相关文章:

php - 使用sed从php文件中删除注释

linux - 从主机获取文本并格式化输出以便分发

bash - 在 BASH 中转换指数和舍入数

file - Unix shell 中文件路径中的单引号或无引号

linux - 如果模式在 sed linux mint 17 中匹配则删除所有行

regex - 如何使用 sed 匹配任意 URL?

linux - 解析 XML - 从标签中减去值

c# - Mono (Linux) 中带有 Args 的 Process.Run(),不启动进程

linux - 比较具有不同列的两个文件

linux - 什么是接口(interface)标识符