美文网首页工作生活
shell编程之grep

shell编程之grep

作者: hemingkung | 来源:发表于2019-06-30 23:56 被阅读0次

grep和egrep

grep的语法格式:

   grep [option] [pattern] [file1,file2...]
   必须掌握的选项:
   -v            显示不匹配pattern的行                             grep -v python file
   -i            搜索时忽略大小写                                  grep -i python file
   -n            显示行号                                          grep -n python file
   -E            支持扩展的正则表达式                              grep -E "python|PYTHON" file
   -F            不支持正则表达式,按字符串的字面意思进行匹配      grep -F "py.*" file
   -r            递归搜索,当前目录下递归搜索所有                  grep -r love

需要了解的选项

   -c            只输出匹配行的数量,不显示具体内容                grep -c man /etc/man_db.conf
   -w            匹配整词                                          grep -w love file
   -x            匹配整行                                          grep -x "i love java" file
   -l            只列出匹配的文件名,不显示具体匹配行的内容        
   
   
   grep和egrep
   
   grep默认不支持扩展正则表达式,只支持基础正则表达式
   
   使用grep -E 可以支持扩展的正则表达式
   
   使用egrep可以支持扩展正则表达式,与grep -E等价
   
例子:
grgrep "PY" file


grep "py.*" file

grep -E "python|PYTHON" file

egrep "python|PYTHON" file

相关文章

网友评论

    本文标题:shell编程之grep

    本文链接:https://www.haomeiwen.com/subject/nhgicctx.html