在前文中演示了使用awk和sed命令正则查找和替换Makefile文件中的make clean操作规则:把-(RM) (ULT_BIN)和-(RM) (ULT_BIN)这两句写成一句-(RM) (ULT_BIN)
要实现的效果如果上图所示,左边表示之前的Makefile文件,右边是通过本次Shell脚本处理后的Makefile文件,红色部分就是需要点。具体如下:
需求1:
之前这个Makefile脚本在生成多个可执行文件时会调用gen_excbin
包,然后执行*.o
的生成规则,但是由于它前面加了$(CURDIR)/
变量,而%.cpp
和.c
前面没有加,所以不会调用gen_depend
包生成.d依赖文件了,而是按照默认隐含的gcc或者g++编译规则生成.o文件。所以为了批量替换掉虚拟机中项目现有所有的Makefile文件,BZ选择用包含sed
和awk
命令的shell脚本来处理。
需求2:
每调用gen_excbin或者gen_libs包生成可执行文件或者库文件后(即
@
需求3:
替换掉原来的(bin).o为 (CURDIR)/
下面的这份shell脚本比较简单,直接运行./sedawkfindreplace3.sh
即可。同前文的脚本框架一样,这里先使用for ... in
的Makefile文件遍历中,然后利用了awk命令的正则匹配查找、替换操作,然后是sed命令执行正则匹配查找、替换以及追加操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #!/bin/bash # FileName: sedawkfindreplace3.sh # Description: Basic usage of sed and awk command such as find and replace words in the regular expression. # Simple Usage: ./sedawkfindreplace1.sh # (c) 2017.5.22 vfhky https://typecodes.com/linux/sedawkfindreplace3.html # https://github.com/vfhky/shell-tools/blob/master/filehandle/sedawkfindreplace3.sh # Dir to be handled for windows. # SRC_DIR="/e/typecodes.com/vfhky/src" # Dir to be handled for Linux. SRC_DIR="/home/vfhky/src" # The makefile you want to modify. SEARCH_NAME="Makefile*" # The maximum depth of the dirs where files such as Makefile you're dealing with lies in. MAXDEPTH=10 # Get the target files you want to modify. ALL_MAKEFILE=$(find ${SRC_DIR} -maxdepth ${MAXDEPTH} -type f -name "${SEARCH_NAME}") # Traverse the target files. for FILE in ${ALL_MAKEFILE} do echo -e 'Handling file='${FILE}'' #### Ways recommended: find "-$(RM) $(ULT_BIN)" by awk command. #awk '/\$(CURDIR)\/\%.o\: \%.cpp/{printf( "%s:%d: %s\n", FILENAME, NR, $0) }' ${FILE} #awk '/-lprint$/{printf( "%s:%d: %s\n", FILENAME, NR, $0) }' ${FILE} #### replace "-$(RM) $(ULT_BIN)" with "-$(RM) $(ULT_BIN) $(ULT_LIBS)" using awk command. # awk '{sub(/-\$(RM) \$(ULT_BIN)/,"-\$(RM) \$(ULT_BIN) \$(ULT_LIBS)"); print $0}' ${FILE} > ${FILE}.tmp; cp ${FILE}.tmp ${FILE}; rm -rf ${FILE}.tmp #### find "-$(RM) $(ULT_BIN)" by sed command. #sed -n "/\$(CURDIR)\/\%.o: \%.c$/p" ${FILE} #### Ways recommended: Step1. replace "-$(RM) $(ULT_BIN)" with "-$(RM) $(ULT_BIN) $(ULT_LIBS)" using sed command. ## 替换 sed -i 's#\$(CURDIR)\/\%.o: \%.cpp$#\$(CURDIR)\/\%.o: \$(CURDIR)\/\%.cpp#g' ${FILE} ## 替换 sed -i 's#\$(CURDIR)\/\%.o: \%.c$#\$(CURDIR)\/\%.o: \$(CURDIR)\/\%.c#g' ${FILE} ## 替换 sed -i 's#$(bin).o#\$(CURDIR)\/$(bin).o#g' ${FILE} ## 追加(以 $$@ 结尾) sed -i '/ -o \$\$\@$/ a\ @echo \"========================Success========================\"' ${FILE} ## 追加(以 $$@ $$^) 结尾) sed -i '/\$\$\@ \$\$\^)$/ a\ @echo \"========================Success========================\"' ${FILE} done exit 0 |
---|
^) 结尾) sed -i '/\
BZ在C/C++工程在/home/vfhky/src,执行这个脚本得到的如下图所示的结果:
在find
命令的某个参数使用正则,那么最好对这个对数加上双引号,正如上面的代码"${SEARCH_NAME}"
所示,否则会出现下面的错误:
find: paths must precede expression: Makefile1
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
目前已经把这个脚本放在Github了,地址是https://github.com/vfhky/shell-tools,以后脚本的更新或者更多好用的脚本也都会加入到这个工程中。