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


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

# grep 命令的选项有哪些?

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: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>

# 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 ‘(?<=CN=Android).*(?=)’
命令说明:此处使用的是-P选项,(?<=xxx).*(?=xxx) 这个属于perl语言的正则表达式,(?<=xxx)表示xxx作为开始位置,(?=xxx)表示xxx作为结束位置
输出结果:
Issuer: EMAILADDRESS=android11@android.com, CN=Android, 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=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

示例2:
期望结果:提取包含CN=xxx的行,并且只打印CN=xxx
命令:cat apkinfo.txt | grep -Po ‘(?<=android.com, ).*(?=, OU)’
命令说明:-P选项表示使用perl语言的正则表达式,-o选项表示只打印匹配的字串、不打印整行内容。”android.com, “为匹配的开始位置,”, OU”为匹配的结束位置

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

如果去掉-o选项,那么是什么结果呢?
命令:cat apkinfo.txt | grep -P ‘(?<=android.com, ).*(?=, OU)’
输出结果:(输出了包含CN=xxx的整行内容)
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

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

示例3:
期望结果:只输出邮箱中的用户名信息
命令:cat apkinfo.txt | grep -Po ‘(?<=EMAILADDRESS=).*(?=@android.com)’
命令说明:使用-P和-o选项,-o选项表示只输出匹配的字串。(?<=EMAILADDRESS=)表示以EMAILADDRESS=作为开始位置,(?=@android.com)表示以@android.com作为结束位置
输出结果:
android11
android22
android33
android44
android55
android66
android77
android88
android99

 

 

https://blog.csdn.net/ahilll/article/details/82712694 Perl正则表达式超详细教程

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

(?=…):表示从左向右的顺序环视。例如(?=/d)表示当前字符的右边是一个数字时就满足条件
(?!…):表示顺序环视的取反。如(?!/d)表示当前字符的右边不是一个数字时就满足条件
(?<=…):表示从右向左的逆序环视。例如(?<=/d)表示当前字符的左边是一个数字时就满足条件
(?<!)…:表示逆序环视的取反。如(?<!/d)表示当前字符的左边不是一个数字时就满足条件
关于”环视”锚定,最需要注意的一点是匹配的结果不占用任何字符,它仅仅只是锚定位置。

 

原创文章,作者:jamestackk,如若转载,请注明出处:https://blog.ytso.com/270510.html

(0)
上一篇 2022年6月27日
下一篇 2022年6月27日

相关推荐

发表回复

登录后才能评论