定义

依据用户指定的模式(pattern),对指标文件进行过滤,显示被漠视匹配到的行

格局:grep [参数] 匹配内容 [文件]

罕用参数:

  • -v:显示未匹配到的行
  • -i:疏忽大小写
  • -n:显示匹配的行号
  • -c:统计匹配的行数
  • -o:仅显示匹配到的字符串
  • -E:应用 ERE,相当于 egrep

实战利用:

  • 显示含有 root 的行,并显示行号
$ cat testroot root hello rootnewnewrootrootleokatehogwartstringleon$ grep -n root test1:root root hello root4:root5:root
  • 显示不蕴含 root 的行,并显示行号
$ grep -vn root test2:new3:new6:leo7:kate8:hogwart9:string10:leon
  • 查找以 s 结尾的行;查找以 n 结尾的行
$ grep ^s -n test9:string$ grep n$ -n test10:leon