linux 三剑客之grep的十六种用法##
文件目录及内容
![在这里插入图片描述](https://img-blog.csdnimg.cn/088cb65ff95a4c52b59b6c3858d77448.png
一、在单个文件中查询指定字符串
grep ab ./tmp00.txt
二、在多个文件中查找指定字符串(并支持问文件通配符)
grep ab ./*/*
三、过滤多个关键字:“与”和“或”
- 同时包含ad和c
grep ab ./*/* | grep c
- 包含ab或者cd
grep -E "ab|cd" ./*/*
四、查找的过程中忽略大小写(grep -i)
grep -i "abcd" ./*/*
五、匹配完整的单词,而不是子串(grep -w)
grep -w "ab" ./*/*
六、高亮grep的显示结果(grep --color=auto)
grep --color=auto ab ./*/*
七、递归的搜索目录(grep -r)
grep -r "cd"
八、取反搜索结果(grep -v)
grep -v "ab" ./*/*
九、取反(多个)指定模式的匹配结果
grep -v -e "cd" -e "ab" ./*/*
十、只显示匹配命中的文件名称,而不显示具体匹配的内容(grep -l)
grep -l "ab" ./*/*
十一、显示匹配的字符串位置。该位置是相对于整个文件的字节位置,不是行数(grep -b)
grep -b "ab" ./*/*
十二、显示匹配的字符串在文件中的行数(grep -n)
grep -n "ab" ./*/*
十三、显示所匹配行的前后行信息(grep -A(after),grep -B(before),grep -C(可省略after+before))
grep -nw -A2 abcd ./*/* #后2行
grep -nw -B2 abcd ./*/* #前2行
grep -nw -C2 abcd ./*/* #前后2行
grep -nw -2 abcd ./*/* #前后2行
十四、正则匹配以a开头(以d结尾)的字符
grep "^a" ./*/* #以a开头
grep "d\$" ./*/* #以d结尾
grep "^a..d$" ./*/* #以a开头,以d结尾
十五、统计复合结果条件的行数(grep -c)
grep -c "ab" ./*/*
十六、,只打印匹配的pattern(grep默认会打印包含匹配pattern的一整行) grep -o
grep -o "ab" ./*/*
参考链接:
# https://www.cnblogs.com/mrwhite2020/p/16632978.html
# https://blog.csdn.net/HandsomeHong/article/details/127281694