linux-grep

grep --help

[root@ora ~]# grep --help

Usage: grep [OPTION]... PATTERN [FILE]...

Search for PATTERN in each FILE or standard input.

PATTERN is, by default, a basic regular expression (BRE).

Example: grep -i 'hello world' menu.h main.c

 

Regexp selection and interpretation:

-E, --extended-regexp PATTERN is an extended regular expression (ERE) 模式是一个扩展正则表达式

-F, --fixed-strings PATTERN is a set of newline-separated fixed strings PATTERN是一组以新行分隔的固定字符串

-G, --basic-regexp PATTERN is a basic regular expression (BRE) 模式是一个基本的正则表达式(BRE)

-P, --perl-regexp PATTERN is a Perl regular expression PATTERN是一个Perl正则表达式

-e, --regexp=PATTERN use PATTERN for matching 使用模式进行匹配

-f, --file=FILE obtain PATTERN from FILE 从文件中获取模式

-i, --ignore-case ignore case distinctions 忽略大小写的区别

-w, --word-regexp force PATTERN to match only whole words 强制pattern只匹配整个单词,精确匹配整个单词

-x, --line-regexp force PATTERN to match only whole lines 强制pattern只匹配整行,精确匹配整行

-z, --null-data a data line ends in 0 byte, not newline

 

Miscellaneous:

-s, --no-messages suppress error messages

-v, --invert-match select non-matching lines 反相匹配

-V, --version print version information and exit

--help display this help and exit

--mmap ignored for backwards compatibility

 

Output control:

-m, --max-count=NUM stop after NUM matches

-b, --byte-offset print the byte offset with output lines 额外打印字节偏移量

-n, --line-number print line number with output lines 额外打印行号

--line-buffered flush output on every line

-H, --with-filename print the filename for each match

-h, --no-filename suppress the prefixing filename on output

--label=LABEL print LABEL as filename for standard input

-o, --only-matching show only the part of a line matching PATTERN 只显示匹配的部分

-q, --quiet, --silent suppress all normal output 安静模式,不输出

--binary-files=TYPE assume that binary files are TYPE;

TYPE is `binary', `text', or `without-match'

-a, --text equivalent to --binary-files=text

-I equivalent to --binary-files=without-match

-d, --directories=ACTION how to handle directories;

ACTION is `read', `recurse', or `skip'

-D, --devices=ACTION how to handle devices, FIFOs and sockets;

ACTION is `read' or `skip'

-R, -r, --recursive equivalent to --directories=recurse

--include=FILE_PATTERN search only files that match FILE_PATTERN

--exclude=FILE_PATTERN skip files and directories matching FILE_PATTERN

--exclude-from=FILE skip files matching any file pattern from FILE

--exclude-dir=PATTERN directories that match PATTERN will be skipped.

-L, --files-without-match print only names of FILEs containing no match 打印不能匹配的文件名

-l, --files-with-matches print only names of FILEs containing matches 打印能匹配上的文件名

-c, --count print only a count of matching lines per FILE 打印匹配的行数

-T, --initial-tab make tabs line up (if needed)

-Z, --null print 0 byte after FILE name

 

Context control:

-B, --before-context=NUM print NUM lines of leading context 附加打印后几行

-A, --after-context=NUM print NUM lines of trailing context 附加打印前几行

-C, --context=NUM print NUM lines of output context 附加打印前后几行

-NUM same as --context=NUM

--color[=WHEN],

--colour[=WHEN] use markers to highlight the matching strings;

WHEN is `always', `never', or `auto'

-U, --binary do not strip CR characters at EOL (MSDOS)

-u, --unix-byte-offsets report offsets as if CRs were not there (MSDOS)

 

`egrep' means `grep -E'. `fgrep' means `grep -F'.

Direct invocation as either `egrep' or `fgrep' is deprecated. 直接调用egrep和fgrep已经废弃了,使用grep –e /-f

With no FILE, or when FILE is -, read standard input. If less than two FILEs

are given, assume -h. Exit status is 0 if any line was selected, 1 otherwise;

if any error occurs and -q was not given, the exit status is 2.

 

匹配得上返回0,匹配不上返回1,有错误返回2

示例

准备材料

[root@node0 tmp]# cat passwd

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

-i 忽略大小写

[root@node0 tmp]# cat passwd |grep -i ROOT

1 root:x:0:0:root:/root:/bin/bash

-w 精确匹配

[root@node0 tmp]# cat passwd |grep roo

1 root:x:0:0:root:/root:/bin/bash

[root@node0 tmp]# cat passwd |grep -w roo

-x 匹配整行

[root@node0 tmp]# cat passwd |grep roo

1 root:x:0:0:root:/root:/bin/bash

[root@node0 tmp]# cat passwd |grep -x roo

-v 反向匹配

[root@node0 tmp]# cat passwd |grep roo

1 root:x:0:0:root:/root:/bin/bash

[root@node0 tmp]# cat passwd |grep -v roo

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

-n 显示行号

[root@node0 tmp]# cat passwd |grep -v -n roo

2:2 bin:x:1:1:bin:/bin:/sbin/nologin

3:3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4:4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5:5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6:6 sync:x:5:0:sync:/sbin:/bin/sync

7:7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8:8 halt:x:7:0:halt:/sbin:/sbin/halt

9:9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10:10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

11:9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

-c 匹配的行的数量

[root@node0 tmp]# cat passwd |grep -c mail

2

-B 附加打印前几行

root@node0 tmp]# cat passwd |grep -B 4 shutdown

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

-A 附加打印后几行

[root@node0 tmp]# cat passwd |grep -A 2 shutdown

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

-C 附加打印前后几行

[root@node0 tmp]# cat passwd |grep -C 2 shutdown

5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

去除文件中的空白行

cat passwd |grep –v '^$'

去除文件中的注释行

cat passed |grep –v '^#'

[root@node0 tmp]# cat passwd

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

 

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

#5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@node0 tmp]# cat passwd |grep -v '^$'

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

#5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@node0 tmp]# cat passwd |grep -v '^$'|grep -v '^#'

1 root:x:0:0:root:/root:/bin/bash

2 bin:x:1:1:bin:/bin:/sbin/nologin

3 daemon:x:2:2:daemon:/sbin:/sbin/nologin

4 adm:x:3:4:adm:/var/adm:/sbin/nologin

6 sync:x:5:0:sync:/sbin:/bin/sync

7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

8 halt:x:7:0:halt:/sbin:/sbin/halt

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10 uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

[root@node0 tmp]#

posted @ 2020-06-08 17:37  jeancheng  阅读(129)  评论(0编辑  收藏  举报