linux grep命令的-P和-o选项的作用

linux grep命令的-P和-o选项的作用

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
-G, –basic-regexp PATTERN is a basic regular expression (BRE)
-P, –perl-regexp PATTERN is a Perl regular expression
-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
-x, –line-regexp force PATTERN to match only whole lines
-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 deprecated no-op; evokes a warning

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 file name for each match
-h, –no-filename suppress the file name prefix on output
–label=LABEL use LABEL as the standard input file name prefix
-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, –recursive like –directories=recurse
-R, –dereference-recursive likewise, but follow all symlinks
–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/Windows)
-u, –unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)

‘egrep’ means ‘grep -E’. ‘fgrep’ means ‘grep -F’.

Direct invocation as either ‘egrep’ or ‘fgrep’ is deprecated.

When FILE is -, read standard input. With no FILE, read . if a command-line
-r is given, – otherwise. If fewer than two FILEs are given, assume -h.

Exit status is 0 if any line is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page:

grep的-P选项作用

-P, –perl-regexp PATTERN is a Perl regular expression

-P选项表示可以使用perl语言的正则规则

grep的-o选项作用

-o, –only-matching show only the part of a line matching PATTERN

-o选项表示只输出匹配的字串,而不是整行内容

示例的apkinfo.txt 文件内容:
Issuer: EMAILADDRESS=android11@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android22@android.com, CN=OsuLogin, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android33@android.com, CN=ServiceWifiResources, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android44@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android55@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android66@android.com, CN=OsuLogin, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android77@android.com, CN=ServiceWifiResources, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android88@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US
Issuer: EMAILADDRESS=android99@android.com, CN=Android, OU=Android, O=Android, L=Mountain View, ST=California, C=US

示例1:
期望结果:提取包含CN=Android的行
命令:cat apkinfo.txt | grep -P ‘(?

示例2:
期望结果:提取包含CN=xxx的行,并且只打印CN=xxx
命令:cat apkinfo.txt | grep -Po ‘(?

输出结果:
CN=Android
CN=OsuLogin
CN=ServiceWifiResources
CN=Android
CN=Android
CN=OsuLogin
CN=ServiceWifiResources
CN=Android
CN=Android

如果去掉-o选项,那么是什么结果呢?
命令:cat apkinfo.txt | grep -P ‘(?

这就是-o参数的作用,-o选项表示只打印匹配的字串、不打印整行内容

示例3:
期望结果:只输出邮箱中的用户名信息
命令:cat apkinfo.txt | grep -Po ‘(?

环视锚定(断言)
“环视”锚定,即lookaround anchor,也称为”零宽断言”,它表示匹配的是位置,不是字符。

(?=…):表示从左向右的顺序环视。例如(?=\d)表示当前字符的右边是一个数字时就满足条件
(?!…):表示顺序环视的取反。如(?!\d)表示当前字符的右边不是一个数字时就满足条件
(?

Original: https://www.cnblogs.com/onelikeone/p/16415452.html
Author: 行走的思想
Title: linux grep命令的-P和-o选项的作用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/8576/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

免费咨询
免费咨询
扫码关注
扫码关注
联系站长

站长Johngo!

大数据和算法重度研究者!

持续产出大数据、算法、LeetCode干货,以及业界好资源!

2022012703491714

微信来撩,免费咨询:xiaozhu_tec

分享本页
返回顶部