grep 运算符
logrep
is very useful tool for text search and pattern matching. We have all ready provided tutorial and examples about grep
and egrep
. In this tutorial we will look grep
command or
, and
, not
logic operations in detail.
lo grep
对于文本搜索和模式匹配非常有用。 我们已经准备好提供有关grep
和egrep
教程和示例。 在本教程中,我们将详细查看grep
命令or
, and
, not
逻辑运算。
示例文字 (Example Text)
We will use following text during tutorial for grep
operations. This is the wage list of Manchester United Football Team.
在教程中,我们将使用以下文本进行grep
操作。 这是曼联足球队的工资表。
David de Gea 26 £200,000 2 Years (2019)
Sergio Romero 30 £50,000 4 Years (2021)
Marcos Rojo 27 £70,000 2 Years (2019)
Phil Jones 25 £50,000 2 Years (2019)
Chris Smalling 27 £80,000 2 Years (2019)
Eric Bailly 23 £75,000 3 Years (2020)
或逻辑 (OR Logic)
or
logic matches all lines if one of the pattern match. We will use \|
to specify OR logic. In this example we will look players those age 23
OR 30
.
如果其中一种模式匹配,则逻辑or
逻辑匹配所有行。 我们将使用\|
指定或逻辑。 在此示例中,我们将研究23
岁或30
岁的玩家。
$ grep "23\|30" manchester.txt
或与扩展Grep(OR with Extended Grep)
Another way to implement OR logic is using grep
command extended option with -E
. We will specify OR logic with |
. In this example we will look players those age 23
OR 30
.
实现OR逻辑的另一种方法是将grep
命令扩展选项与-E
。 我们将用|
指定OR逻辑。 在此示例中,我们将研究23
岁或30
岁的玩家。
$ grep -E "23|30" manchester.txt
或与Egrep (OR with Egrep)
Another tool used to implement OR logic is egrep
. We will use |
operator again. In this example we will look players those age 23
OR 30
.
用于实现OR逻辑的另一个工具是egrep
。 我们将使用|
再次操作。 在此示例中,我们将研究23
岁或30
岁的玩家。
$ egrep "23|30" manchester.txt
与逻辑 (AND Logic)
AND logic will match lines those have all provided patterns. We will use .*
for AND operator. In this example we will list players those contract is 2 years and age is 27.
AND逻辑将匹配具有所有提供的模式的行。 我们将.*
用于AND运算符。 在此示例中,我们将列出合同为2岁且年龄为27岁的球员。
$ grep "27.*2 Years" manchester.txt
与多个Grep(AND with Multiple Grep)
Another implementation is using multiple grep commands to filter given patterns. In this example we will list players those contract is 2 years and age is 27.
另一种实现是使用多个grep命令来过滤给定的模式。 在此示例中,我们将列出合同为2岁且年龄为27岁的球员。
$ grep "2 Years" manchester.txt | grep 27
非逻辑 (NOT Logic)
NOT logic is used to get results those do not matched given pattern. We will use -v
option for grep. In this example we will list players those do not 27 years old.
NOT逻辑用于获取与给定模式不匹配的结果。 我们将对grep使用-v
选项。 在此示例中,我们将列出未满27岁的球员。
$ grep -v "27" manchester.txt
不适用于多个Grep条件(NOT with Multiple Grep Conditions)
We can implement NOT logic multiple times with multiple grep
commands. We will pipe multiple grep commands in bash. In this example we will list players those is not 27 years old and not have 2 Years contract.
我们可以使用多个grep
命令多次实现NOT逻辑。 我们将在bash中通过管道传递多个grep命令。 在此示例中,我们将列出未满27岁且没有2年合同的球员。
$ grep -v "27" manchester.txt | grep -v "2 Years"
翻译自: https://www.poftut.com/linux-grep-not-operator-logic-examples/
grep 运算符