在注释和空行之后,只在Makefile中插入一行

时间:2013-07-24 23:38:22

标签: insert replace sed grep


I have a scenario where in I need to change thousands of Makefiles.
I need to insert a line in Makefile and it should be the first effect code
(Ignore comments and empty lines if found in the beginning of the file).

In the below example, I need to insert a line NEWLINE=1 in the Makefile.

Source Makefile
---------------

# copy right info
(empty line here).
# The file is mainly for ....
(empty file again here)
# note


all:
gcc sample.c

Target Makefile
---------------

# copy right info
(empty line here).
# The file is mainly for ....
(empty file again here)
# note

NEWLINE= 1

all:
gcc sample.c

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

触摸〜/ tmp
chmod 777~ / tmp
echo sed -i \'$(grep -vnE“^ $ | ^#”Makefile | head -1 | cut -f1 -d“ :“)iNEWLINE = 1 \'Makefile> 〜/ TMP
&安培;&安培; 〜/ TMP