写了一个bash+gawk小脚本,遍历一个目录下的所有文件(包括子文件夹里的文件),看文件中是否存在要查找的字符串,如果有则列出文件名称和字符串所在位置的行的内容。
- #!/bin/bash
- # coded by jackyvan (http://blog.youkuaiyun.com/jackyvan)
- #
- if [ "$#" -lt 2 ] #判断参数
- then
- echo "need 2 arguments at least!"
- echo "*************************"
- echo "1st for seached path"
- echo "2nd for string pattern"
- echo "3rd for showing line string"
- exit
- fi
- showline=0 #variable for showing line string ,0 indicate not showing
- if [ "$#" -eq 3 ]
- then
- showline=1
- fi
- ####### find with gawk working############################
- find "$1" -type f -exec gawk 'BEGIN{count=0 } {if($0~/'"$2"'/){result[count++]="*****"FNR":"$0}}END{if(count>0){print(FILENAME); if('"$showline"'=="1"){for(i=0;i<count;++i){print result[i];}}}}' {} /; 2>/dev/null
本文介绍了一个使用Bash与Gawk编写的实用脚本,该脚本能够递归地搜索指定路径下的所有文件(包括子目录),查找包含特定字符串的目标文件,并输出这些文件的名称及匹配字符串所在的行。
2262

被折叠的 条评论
为什么被折叠?



