grep 运算符_Linux Grep或,与,非运算符和逻辑示例

grep 运算符

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对于文本搜索和模式匹配非常有用。 我们已经准备好提供有关grepegrep教程和示例。 在本教程中,我们将详细查看grep命令orandnot逻辑运算。

示例文字 (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
OR Logic
OR Logic
或逻辑

或与扩展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
OR with Extended Grep
OR with Extended Grep
或与扩展Grep

或与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
OR with Egrep
OR with Egrep
或与Egrep

与逻辑 (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
AND Logic
AND Logic
与逻辑

与多个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
AND with Multiple Grep
AND with Multiple Grep
与多个Grep

非逻辑 (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
NOT Logic
NOT Logic
非逻辑

不适用于多个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"
NOT with Multiple Grep Conditions
NOT with Multiple Grep Conditions
不适用于多个Grep条件
LEARN MORE  Php - Operators
了解更多信息-运营商

翻译自: https://www.poftut.com/linux-grep-not-operator-logic-examples/

grep 运算符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值