正则表达式 grep_如何在Grep中使用正则表达式(正则表达式)?

正则表达式 grep

正则表达式 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 regexfor 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
Start Of Line
Start Of Line
线的起点

比赛结束(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$'
End Of Line
End Of Line
行结束

包含给定文本的行(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 Uppercase 
Match Uppercase 
匹配大写

匹配小写字母(Match Lowercase Letters)

We can match uppercase letters with [az] or [[:lower:]] like below.

我们可以将大写字母与[az][[:lower:]]匹配,如下所示。

$ egrep '[[:lower:]]' /etc/passwd
Match Lowercase
Match Lowercase
匹配小写

忽略大小写匹配(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 Ignoring Case
Match Ignoring Case
忽略比赛案例

匹配任何单个字符(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 .

我们可以使用| 。 在此示例中,我们将匹配rootismail

$ 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
Match Digits
Match Digits
比赛位数
/etc/passwd
End Of Line
End Of Line
行结束
LEARN MORE  How To Get Query String From Url With Javascript?
了解更多信息如何使用Javascript从网址获取查询字符串?

翻译自: https://www.poftut.com/how-to-use-regex-regular-expression-with-grep/

正则表达式 grep

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值