如何在Linux中的文本文件中Grep多个字符串,模式或正则表达式?

grep  provides a lot of features to match strings, patterns or regex in a given text. One of the most used feature is to match two or more, multiple string, patterns or regex. In this tutorial we will look different examples about these features. If you need more general tutorial about regex please look following article.

grep提供了许多功能来匹配给定文本中的字符串,模式或正则表达式。 最常用的功能之一是匹配两个或多个,多个字符串,模式或正则表达式。 在本教程中,我们将查找有关这些功能的不同示例。 如果您需要有关regex更多常规教程,请阅读以下文章。

How To Use Regular Expression – Regex In Bash Linux?

如何在Bash Linux中使用正则表达式– Regex?

Grep -E或Egrep (Grep -E or Egrep)

Before starting examples we look different commands which provides same functionality. We can use grep  command with -E  option or egrep command which is the alias of the grep -E . They are both the same. In this tutorial we will follow grep -E. -E means extended grep which will enable extended regular expression features to use.

在开始示例之前,我们先看一下提供相同功能的不同命令。 我们可以使用grep命令-E选项或egrep命令,这是别名grep -E 。 他们都是一样的。 在本教程中,我们将遵循grep -E-E表示扩展grep,它将允许使用扩展的正则表达式功能。

匹配多个字符串 (Match Multiple Strings)

We will start with the simplest example. We will match given multiple strings inside a given text. In this example we will use a file named data.txt as a text. The matching strings will be ismail  and ali .

我们将从最简单的示例开始。 我们将在给定文本内匹配给定的多个字符串。 在此示例中,我们将使用名为data.txt的文件作为文本。 匹配的字符串将是ismailali

$ grep -E "ismail|ali" data.txt
Match Multiple Strings
Match Multiple Strings
匹配多个字符串

匹配多个模式或正则表达式(Match Multiple Pattern or Regex)

Now we want to use multiple regular expression or pattern in our match term. We can specify standard regular expression with the same way. We will change some letters with . in this example.

现在,我们要在匹配项中使用多个正则表达式或模式。 我们可以用相同的方式指定标准正则表达式。 我们将使用来更改一些字母. 在这个例子中。

$ grep -E "is.ail|al." data.txt
Match Multiple Pattern or Regex
Match Multiple Pattern or Regex
匹配多个模式或正则表达式

匹配IP地址或域名(Match IP Address or Domain Name)

Now we will look useful example which provides IP address or domain names. This may be a regular operation while searching in log files. We will use [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}  as IP Address regex and  \.[a-z]{1,20}\.[a-z]+ as domain name regex.

现在我们来看一下提供IP地址或域名的有用示例。 搜索日志文件时,这可能是常规操作。 我们将使用[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}作为IP地址正则表达式和\.[az]{1,20}\.[az]+作为域名正则表达式。

$ grep -E "([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(\.[a-z]{1,20}\.[a-z]+)" data.txt
Match IP Address or Domain Name
Match IP Address or Domain Name
匹配IP地址或域名

匹配多个文件(Match In Multiple Files)

We have also the ability to search and match in multiple files. We can use bash glob feature for this. We will use * for the all names and .txt to match text files in this example.

我们还可以搜索和匹配多个文件。 我们可以为此使用bash glob功能。 在此示例中,我们将使用*表示所有名称,并使用.txt匹配文本文件。

$ grep -E "ismail|ali" *.txt
Match In Multiple Files
Match In Multiple Files
匹配多个文件

As we can see matched files also printed with the matched text.

如我们所见,匹配的文件也打印有匹配的文本。

LEARN MORE  Linux xargs Command Tutorial With Examples
了解更多带有示例Linux xargs命令教程

翻译自: https://www.poftut.com/grep-multiple-strings-patterns-regex-text-file-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值