grep搜索文本

正则匹配:this

grep -E "[a-z]+"递归

只输出匹配到的文本:test

echo this is a line. | grep -o -E "[a-z]+\."file

统计匹配到的行数:搜索

grep -c grep

匹配以外的行:统计

grep -v样式

多级目录中递归搜索:文件

grep  "text" . -R -n

忽略大小写:

grep -i

匹配多个样式:

grep -e "this" -e "line"

grep中包括货排除文件:

grep "main()" . -r --include *.{c,cpp}

grep "main()" . -r --exclude "README"

删除grep出的文件:(xargs -0表示以0值结尾的,防止有些文件名中有空格,被看成两个文件名)

grep "test" file* -lz |xargs -0 rm

静默输出:

grep -q

打印匹配文本以前货以后的行:

seq 10 | grep 5 -A 3     //以前

seq 10 | grep 5 -B 3  //以后

seq 10 | grep 5 -C 3  //先后

 

摘自:《Linux Shell 脚步攻略》

相关文章
相关标签/搜索