Back
Featured image of post 文本处理三剑客之grep

文本处理三剑客之grep

grep 与 egrep 的参数说明及使用

grep

语法格式:

  1. grep [option] [pattern] [file1, file2]
  2. command | grep [option] [pattern]

grep 参数:

参数 含义
-v 不显示匹配信息
-i 忽略大小写
-n 显示行号
-r 递归搜索
-E 支持扩展正则表达式 (egrep)
-F 不按正则表达式匹配
以下为不常用参数
-c 只显示匹配行总数
-w 匹配整词
-x 匹配整行
-l 只显示文件名, 不显示内容
-s 不显示错误信息
$ grep 'py.*' file   # grep 支持正则表达式

$ grep -E "python|PYTHON" file  # 支持扩展正则表达式

$ grep -F "py.*" file  # 会按原始语义搜索

egrep

egrep 与 grep -E 等价

$ grep -E 'python|PYTHON' file

$ egrep 'python|PYTHON' file