【问题标题】:Insert a line only one time in Makefile after the comments and empty lines在 Makefile 中仅在注释和空行之后插入一行
【发布时间】:2013-07-24 16:12:18
【问题描述】:
我有一个场景,我需要更改数千个 Makefile。
我需要在 Makefile 中插入一行,它应该是第一个效果代码
(如果在文件开头找到 cmets 和空行,则忽略)。
在下面的示例中,我需要在Makefile中插入一行NEWLINE=1。
Source Makefile
---------------
#版权信息
(此处为空行).
# 文件主要用于....
(此处再次为空文件)
# note
all:
gcc 示例.c
目标 Makefile
---------------
# 版权信息
(此处为空行)。
# 该文件主要用于....
(这里又是空文件)
# note
NEWLINE= 1
all:
gcc sample.c上一页>
【问题讨论】:
标签:
insert
replace
sed
grep
【解决方案1】:
awk 的一种方式:
awk '/all:/{print "NEWLINE =1\n\n" $0;next}1' source > tmp && mv tmp source
或
使用sed(文件内替换):
sed -i 's/all:/NEWLINE= 1\n\n&/' source
【解决方案2】:
touch ~/tmp
chmod 777 ~/tmp
echo sed -i \'$(grep -vnE "^$|^#" Makefile | head -1 | cut -f1 -d ":" )iNEWLINE=1\' Makefile > ~/tmp
&& ~/tmp