7 Linux Grep OR, Grep AND, Grep NOT Operator Examples

Unix grep 命令详解

by Ramesh Natarajan on October 21, 2011

http://www.thegeekstuff.com/2011/10/grep-or-and-not-operators/


Question: Can you explain how to use OR, AND and NOT operators in Unix grep command with some examples?

Answer: In grep, we have options equivalent to OR and NOT operators. There is no grep AND opearator. But, you can simulate AND using patterns. The examples mentioned below will help you to understand how to use OR, AND and NOT in Linux grep command.

The following employee.txt file is used in the following examples.

$ cat employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500
500  Randy   Manager    Sales       $6,000

You already knew that grep is extremely powerful based on these grep command examples.

Grep OR Operator

Use any one of the following 4 methods for grep OR. I prefer method number 3 mentioned below for grep OR operator.

1. Grep OR Using \|

If you use the grep command without any option, you need to use \| to separate multiple patterns for the or condition.

grep 'pattern1\|pattern2' filename

For example, grep either Tech or Sales from the employee.txt file. Without the back slash in front of the pipe, the following will not work.

$ grep 'Tech\|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000

2. Grep OR Using -E

grep -E option is for extended regexp. If you use the grep command with -E option, you just need to use | to separate multiple patterns for the or condition.

grep -E 'pattern1|pattern2' filename

For example, grep either Tech or Sales from the employee.txt file. Just use the | to separate multiple OR patterns.

$ grep -E 'Tech|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000

3. Grep OR Using egrep

egrep is exactly same as ‘grep -E’. So, use egrep (without any option) and separate multiple patterns for the or condition.

egrep 'pattern1|pattern2' filename

For example, grep either Tech or Sales from the employee.txt file. Just use the | to separate multiple OR patterns.

$ egrep 'Tech|Sales' employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000

4. Grep OR Using grep -e

Using grep -e option you can pass only one parameter. Use multiple -e option in a single command to use multiple patterns for the or condition.

grep -e pattern1 -e pattern2 filename

For example, grep either Tech or Sales from the employee.txt file. Use multiple -e option with grep for the multiple OR patterns.

$ grep -e Tech -e Sales employee.txt
100  Thomas  Manager    Sales       $5,000
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
500  Randy   Manager    Sales       $6,000

Grep AND

5. Grep AND using -E ‘pattern1.*pattern2′

There is no AND operator in grep. But, you can simulate AND using grep -E option.

grep -E 'pattern1.*pattern2' filename
grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename

The following example will grep all the lines that contain both “Dev” and “Tech” in it (in the same order).

$ grep -E 'Dev.*Tech' employee.txt
200  Jason   Developer  Technology  $5,500

The following example will grep all the lines that contain both “Manager” and “Sales” in it (in any order).

$ grep -E 'Manager.*Sales|Sales.*Manager' employee.txt

Note: Using regular expressions in grep is very powerful if you know how to use it effectively.

6. Grep AND using Multiple grep command

You can also use multiple grep command separated by pipe to simulate AND scenario.

grep -E 'pattern1' filename | grep -E 'pattern2'

The following example will grep all the lines that contain both “Manager” and “Sales” in the same line.

$ grep Manager employee.txt | grep Sales
100  Thomas  Manager    Sales       $5,000
500  Randy   Manager    Sales       $6,000

Grep NOT

7. Grep NOT using grep -v

Using grep -v you can simulate the NOT conditions. -v option is for invert match. i.e It matches all the lines except the given pattern.

grep -v 'pattern1' filename

For example, display all the lines except those that contains the keyword “Sales”.

$ grep -v Sales employee.txt
200  Jason   Developer  Technology  $5,500
300  Raj     Sysadmin   Technology  $7,000
400  Nisha   Manager    Marketing   $9,500

You can also combine NOT with other operator to get some powerful combinations.

For example, the following will display either Manager or Developer (bot ignore Sales).

$ egrep 'Manager|Developer' employee.txt | grep -v Sales
200  Jason   Developer  Technology  $5,500
400  Nisha   Manager    Marketing   $9,500

<think>好的,我现在需要帮助用户理解他们在执行kubectl命令时看到的输出,特别是关于POD_MEMORY_LIMIT和POD_CPU_LIMIT显示为“node allocatable”的情况。用户之前询问过Rook安装的Ceph默认的MON和MGR资源限制,现在他们执行了提供的命令,但结果似乎与预期不同。 首先,我需要回顾之前的回答,确认默认情况下Rook为MON和MGR设置的资源请求和限制。用户之前得到的回复中提到,默认情况下MON和MGR有特定的CPU和内存请求及限制。但用户现在的命令输出显示这些值被设置为节点的可分配资源,请求为0,这可能意味着实际没有设置显式的限制。 接下来,我需要分析用户提供的输出。用户运行了`kubectl describe pod`并过滤了包含“limit”的部分,结果显示POD_MEMORY_LIMIT和POD_CPU_LIMIT指向节点的可分配资源,而请求部分为0。这可能说明Rook在默认情况下并没有为这些Pod设置显式的资源限制,而是依赖于节点的可用资源。这与我之前的回答可能存在的差异需要澄清,可能之前的回答是基于特定版本或配置,而用户的实际环境可能不同。 然后,我需要考虑用户可能的疑问。用户可能困惑于为什么实际结果与之前介绍的默认值不符。这可能是因为用户使用的Rook版本不同,或者集群配置覆盖了默认值。此外,用户可能需要了解如何正确查看资源限制,或者是否需要手动设置这些值以确保稳定性。 我还需要检查用户提供的命令是否正确。用户使用了`grep -A 5 "limit"`,但输出的内容可能包含了环境变量而非资源限制部分。正确的做法应该是查找“Limits”和“Requests”部分,而不是环境变量中的POD_MEMORY_LIMIT等,这可能是容器内的环境变量,而非k8s的资源限制配置。 接下来,我需要解释为什么用户看到的输出与预期不同。在Kubernetes中,资源限制和请求是在Pod的spec.containers.resources中定义的,而用户可能查看的是环境变量,这些环境变量可能由Rook设置,用于内部配置,而非实际的k8s资源限制。因此,用户需要检查正确的部分,即使用`kubectl describe pod`后查找资源部分,而不是环境变量。 此外,用户可能没有正确应用Cluster CRD中的资源配置,或者他们的Rook版本默认不设置资源限制。需要建议用户检查他们的CephCluster CRD配置,确认是否显式定义了资源请求和限制。如果没有定义,Rook可能依赖节点的可用资源,这可能导致Pod使用更多的资源,可能引发稳定性问题。 最后,用户可能需要指导如何正确调整这些资源限制,确保MON和MGR有足够的资源,特别是在集群规模扩大或高负载情况下。需要重申之前的建议,即通过修改Cluster CRD来设置明确的资源请求和限制,并应用配置,然后验证更改是否生效。同时提醒用户注意版本差异和监控资源使用情况,以避免节点过载。</think>以下是针对你提供的命令输出及资源限制问题的分析: --- ### **一、现象解析** 你执行的命令输出了**环境变量配置**,而非实际的资源限制(例如 `POD_MEMORY_LIMIT: node allocatable`),这表明: 1. **Rook默认未显式设置MON/MGR的资源限制**,而是依赖**节点可用资源(node allocatable)**。 2. **实际生效的资源限制由Kubernetes动态分配**,而非通过Rook配置文件定义。 --- ### **二、关键点说明** | 字段 | 含义 | 风险提示 | |------------------------|----------------------------------------------------------------------------|--------------------------------------------------------------------------| | `POD_MEMORY_LIMIT` | Pod可使用的最大内存 = 节点可分配内存(`allocatable`) | 若节点内存不足,可能导致OOM(内存溢出) | | `POD_CPU_LIMIT` | Pod可使用的最大CPU = 节点可分配CPU(`allocatable`) | CPU竞争激烈时,可能触发调度延迟或性能下降 | | `requests.memory=0` | 未设置内存请求值,调度器可能将Pod分配到资源不足的节点 | 可能引发资源抢占问题 | --- ### **三、正确查看资源限制的方法** 使用以下命令过滤**实际资源限制配置**(注意区分大小写): ```bash kubectl -n rook-ceph describe pod/rook-ceph-mon-a-5ffc55d9fc-xpthg | grep -A 5 "Resources" ``` **期望输出示例**: ``` Resources: Limits: cpu: 2 memory: 4Gi Requests: cpu: 1 memory: 1Gi ``` 若输出中无此内容,则说明**未显式定义资源限制**。 --- ### **四、解决方案** #### 1. **显式定义资源限制** 在 `CephCluster` CRD 中强制指定资源请求和限制(覆盖默认行为): ```yaml spec: mon: resources: limits: cpu: "2" memory: "4Gi" requests: cpu: "1" memory: "1Gi" mgr: resources: limits: cpu: "1" memory: "2Gi" requests: cpu: "500m" memory: "1Gi" ``` #### 2. **应用配置并验证** ```bash kubectl apply -f cluster.yaml # 验证生效 kubectl -n rook-ceph describe pod/<mon-pod> | grep -A 5 "Resources" ``` #### 3. **注意事项** - **生产环境必须显式设置资源限制**,避免依赖节点动态分配。 - 若使用旧版Rook(如v1.8),需检查Operator配置是否覆盖默认值。 --- ### **五、补充:节点资源分配逻辑** 若未显式设置资源限制,Kubernetes会按以下规则分配: 1. **内存限制** = `节点allocatable内存 - kube-reserved - system-reserved` 2. **CPU限制** = `节点allocatable CPU - 系统守护进程占用` 可通过以下命令查看节点可分配资源: ```bash kubectl describe node <节点名称> | grep Allocatable -A 6 ``` --- 通过显式配置资源限制,可避免因动态分配导致的性能波动或稳定性问题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值