linux 命令之(2)grep

1.NAME

grep, egrep, fgrep, rgrep - print lines matching a pattern

打印匹配到模式的行

2.SYNOPSIS

grep [OPTIONS] PATTERN [FILE…]
grep [OPTIONS] -e PATTERN … [FILE…]
grep [OPTIONS] -f FILE … [FILE…]

3.DESCRIPTION

grep searches for PATTERN in each FILE. A FILE of “-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input. By default, grep prints the matching lines.

grep 命令在每个文件中搜索 PATTERN。 文件 “-” 代表着标准输入。如果没有给出指定的文件(FILE),递归搜索将检查工作目录,非递归搜索将检查标准输入。默认情况下,grep 命令打印匹配到的行。

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

另外 这几个变体egrep, fgrep ,rgrep 分别和grep -E, grep -F, and grep -r等价。不建议使用这些变体,提供这些变体只是为了向后兼容。

4. OPTIONS

4.1 Matcher Selection (匹配器选择)

-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE, see below).

将模式解释为使用扩展的正则表达式

-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE, see below). This is the default.

将模式解释为使用基础的正则表达式

4.2 Matching Control (匹配控制)

-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used multiple times or is combined with the -f (–file) option, search for all patterns given. This option can be used to protect a pattern beginning with“-”.

-e 用来指定多个模式。 例如:-e 模式1 -e 模式2

-f FILE, --file=FILE
Obtain patterns from FILE, one per line. If this option is used multiple times or is combined with the -e (–regexp) option, search for all patterns given. The empty file contains zero patterns, and therefore matches nothing.

-i, --ignore-case
Ignore case distinctions, so that characters that differ only in case match each other.

忽略大小写的区别,以便仅大小写不同的字符进行匹配。

-w, --word-regexp
Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word-constituent characters are letters, digits, and the underscore.This option has no effect if -x is also specified.

4.3 General Output Control(通用输出控制)

4.3.1 -c

-c, --count
Suppress normal output; instead print a count of matching lines for each input file. With the -v, --invert-match option , count non-matching lines.

废止正常的输出,替换为输出匹配到的行。如果带了-v 选项,输出没有匹配到的行。

4.4 Output Line Prefix Control(输出行前缀控制)

4.4.1 -n

-n, --line-number
Prefix each line of output with the 1-based line number within its input file.
输出基于1开始的行号。

4.5 Context Line Control(输出上下文控制)

-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

复合规则的之后的 num 行也显示。

-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (–) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.

复合规则的之前的 num 行也显示。

-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (–) between contiguous groups of matches.With the -o or --only-matching option, this has no effect and a warning is given.

复合规则的之后的前后 num 行也显示。

4.6 File and Directory Selection(文件和路径选择)

-r, --recursive
Read all files under each directory, recursively, following symbolic links only if they are on the command line. Note that if no file operand is given, grep searches the working directory. This is equivalent to the -d recurse option.

4.7 Other Options(其他选项)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值