内容简介:使用sed和awk查找和替换字符串处理Makefile文件(三)
在前文中演示了使用awk和sed命令正则查找和替换Makefile文件中的 make clean 操作规则:把 -$(RM) $(ULT_BIN) 和 -$(RM) $(ULT_BIN) 这两句写成一句 -$(RM) $(ULT_BIN) $(ULT_BIN) ,这篇文件主要是通过 sed 和 awk 命令继续优化该Makefile文件。
1 具体功能需求
要实现的效果如果上图所示,左边表示之前的Makefile文件,右边是通过本次 Shell 脚本处理后的Makefile文件。因此,具体的需求功能如下:
需求1:
之前这个Makefile脚本在生成多个可执行文件时会调用 gen_excbin 包,然后执行 *.o 的生成规则,但是由于它前面加了 $(CURDIR)/ 变量,而 %.cpp 和 .c 前面没有加,所以就 gen_depend 包生成.d依赖文件了。所以为了批量替换掉虚拟机中项目现有所有的Makefile文件,BZ选择用包含 sed 和 awk 命令的shell脚本来处理。
需求2:
每调用 gen_excbin 或者 gen_libs 包生成可执行文件或者库文件后(即 $$@ 和 $$@ $$^) 结尾处)都追加打印一个包含 SUCCESS 文件的行,方便区分生成多个可执行文件时日志不好区分的情形。
需求3:
替换掉原来的 $(bin).o 为 $(CURDIR)/$(bin).o 。
2 shell程序
下面的这份shell脚本比较简单,直接运行 ./sedawkfindreplace3.sh 即可。同前文的脚本框架一样,这里先使用 for ... in 的Makefile文件遍历中,然后利用了awk命令的正则匹配查找、替换操作,然后是sed命令执行正则匹配查找、替换以及追加操作。
#!/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 |
3 脚本测试
BZ在虚拟机的/home/vfhky/shell目录复制了5个错误的Makefile文件,然后先做正则查找测试,结果如下图所示:
4 Linux find 命令中正则
在 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]
5 脚本管理
目前已经把这个脚本放在Github了,地址是 https://github.com/vfhky/shell-tools ,以后脚本的更新或者更多好用的脚本也都会加入到这个工程中。
同时,BZ也把修改后的Makefile文件同步更新到了对应的Github上了,欢迎关注,地址还是之前的: https://github.com/vfhky/General_Makefile 。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First JavaScript Programming
Eric T. Freeman、Elisabeth Robson / O'Reilly Media / 2014-4-10 / USD 49.99
This brain-friendly guide teaches you everything from JavaScript language fundamentals to advanced topics, including objects, functions, and the browser’s document object model. You won’t just be read......一起来看看 《Head First JavaScript Programming》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
Markdown 在线编辑器
Markdown 在线编辑器