grep搜索输入文件包含特定pattern的行。当它找到一个匹配行时,默认输出到标准输出。
grep options pattern input_file_names
Matching Control
-e pattern
--regexp=pattern
指定匹配的正则表达式
-f file
--file=file
指定包含正则表达式的文件。每个pattern一行。
-i
-y
--ignore-case
忽略大小写
-v
--invert-match
选择不匹配的行
-w
--word-regexp
只选择整个单词匹配的行
-x
--line-regexp
只选择整行匹配的行
General Output control
-c
--count
抑制正常的输出,只打印整个文件匹配的行数
-L
--files-without-match
抑制正常输出,只打印不存在匹配行的文件,每个文件在扫描到第一个匹配后停止。
-l
--files-with-matches
抑制正常输出,只打印存在匹配行的文件,每个文件在扫描到第一个匹配后停止。
-m num
--max-count=num
只匹配num次
-o
--only-matching
只打印匹配的部分
-q
--quiet
--silent
不打印任何东西,如果匹配以0退出,即使检测到错误。
-s
--no-messages
抑制错误信息
Output line prefix control
-b
--byte-offset
打印行或者匹配部分的字节偏移,只有在开启打印文件名时,才生效。
-H
--with-filename
打印文件名,当有多个文件时,默认打开
-h
--no-filename
不打印文件名,当只有一个文件时,默认打开
-n
--line-number
打印1-based的行号,只有在开启打印文件名时,才生效。
-T
--initial-tab
将输出部分对齐到tab边界。
Context line control
-A num
--after-context=num
匹配行后面的num行也打印
-B num
--before-context=num
匹配行前面的num行也打印
-C num
-num
--context=num
匹配行前n行和后n行都打印
File and Directory selection
-D action
--devices=action
action有read和skip,对于设备文件,FIFO, socket等,其动作是啥。
-d action
--directories=action
read/skip/recurse
--exclude=glob
跳过后缀匹配glob的文件
--exclude-from=file
通过文件指定跳过的glob
--exclude-dir=glob
跳过后缀匹配glob的目录
-I
对于二进制文件,认为其不包含匹配数据,等价于--binary-files=without-match
--include=glob
只搜索匹配glob的文件
-r
--recursive
搜索目录下的所有文件,但是不跟随符号链接
-R
--dereference-recursive
搜索目录下的所有文件,并且跟随符号链接