正则表达式 grep
grep
is a very popular tool used to match given search patterns in the given text. grep provides a simple glob search but also provides regex support which is very useful for complex search ant matches. In this tutorial, we will examine how to use for regex patterns.
grep
是一种非常流行的工具,用于匹配给定文本中的给定搜索模式。 grep提供了简单的全局搜索,还提供了正则表达式支持,这对于复杂的搜索蚂蚁匹配非常有用。 在本教程中,我们将研究如何将其用于正则表达式模式。
使用Grep启用Regex (Enable Regex with Grep)
By default grep
do not supports regex
patterns. We can enable regex
for grep
with -E
option like below. -E
option means extended grep with advanced features like regex
.
默认情况下, grep
不支持regex
模式。 我们可以使用-E
选项为grep
启用regex
,如下所示。 -E
选项表示具有高级功能的扩展grep,例如regex
。
$ grep -E 'ismail' /etc/passwd
OR we can use alias named egrep
which is the same as grep -E
like below.
或者我们可以使用名为egrep
别名,该别名与grep -E
相同,如下所示。
$ egrep 'ismail' /etc/passwd
比赛开始 (Match Start Of Line)
We can match the start of the line with the ^
sign by using it at the start of the search pattern. In this example, we will list lines which start with d
.
我们可以通过在搜索模式的开头使用^
符号来匹配行的开头。 在此示例中,我们将列出以d
开头的行。
$ egrep '^d' /etc/passwd
比赛结束(Match End Of Line)
We can also match end of the line with the $
sign. We will put $
to the end of line and provide the last character we want to match. In this example we will try to match nologin
at the end of line.
我们还可以将行尾与$
符号进行匹配。 我们将$
放在行尾,并提供我们要匹配的最后一个字符。 在此示例中,我们将尝试在行尾匹配nologin
。
$ egrep 'nologin$'
包含给定文本的行(Lines Containing Given Text)
We can also use it to match the given pattern without any special usage. We will match the term mail
with the grep.
我们也可以使用它来匹配给定的模式,而无需任何特殊用法。 我们将把术语mail
与grep相匹配。
$ egrep 'mail' /etc/passwd
匹配大写字母 (Match Uppercase Letters)
We can match uppercase letters with [AZ]
or [[:upper:]]
like below.
我们可以将大写字母与[AZ]
或[[:upper:]]
进行匹配,如下所示。
$ egrep '[[:upper:]]' /etc/passwd
匹配小写字母(Match Lowercase Letters)
We can match uppercase letters with [az]
or [[:lower:]]
like below.
我们可以将大写字母与[az]
或[[:lower:]]
匹配,如下所示。
$ egrep '[[:lower:]]' /etc/passwd
忽略大小写匹配(Match By Ignoring Case)
We have match upper and lowercase letters in previous examples but what if we need to match by ignoring case with lowercase or uppercase for the given term. We will provide -i
option like below.
在前面的示例中,我们将大写字母和小写字母匹配,但是如果需要通过忽略给定术语的大小写匹配来匹配,该怎么办。 我们将提供如下的-i
选项。
$ egrep -i 'r' /etc/passwd
匹配任何单个字符(Match Any Single Character)
If we want to match any single character we can use .
which means just a character. In this example, we will match r..t
.
如果我们想匹配任何单个字符,我们可以使用.
这意味着一个角色。 在此示例中,我们将匹配r..t
。
$ egrep 'r..t' /etc/passwd
逻辑或多种模式 (Logical OR Multiple Patterns)
We can or multiple patterns with |
. In this example we will match root
or ismail
.
我们可以使用|
。 在此示例中,我们将匹配root
或ismail
。
$ egrep 'root|ismail' /etc/passwd
匹配空白行 (Match Blank Line)
We can match blank line which contains no character even a space by using ^
and $
like below.
我们可以使用^
和$
来匹配空白行,该行甚至不包含任何字符,如下所示。
$ egrep '^
比赛位数 (Match Digits)
We can match lines which contains numbers or digits with [09]
or [[:digit:]]
like below.
我们可以用[09]
或[[:digit:]]
匹配包含数字或数字的行,如下所示。
$ egrep '[[:digit:]]' /etc/passwd
/etc/passwd
翻译自: https://www.poftut.com/how-to-use-regex-regular-expression-with-grep/
正则表达式 grep