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
相关推荐
### Linux 下 `grep` 命令详解 `grep` 是 Linux 下一款强大的文本搜索工具,可以在文件中搜索固定字符串或者使用正则表达式搜索,是日常开发、运维工作中非常实用的一个命令。本文将详细介绍 `grep` 的常用选项及其...
Linux中的`grep`命令是日常系统管理和数据处理中不可或缺的工具,它用于在文本文件或标准输入中搜索指定模式,并打印匹配行。`grep`的名字来源于"Global Regular Expression Print",意味着全局正则表达式打印。现在...
Linux中的`grep`命令是文本处理工具中的重要一员,它在系统管理和日常工作中发挥着巨大的作用。`grep`源自于“global regular expression print”,即全局正则表达式打印,它的主要功能是从输入流(通常是文件或管道...
Linux grep 命令是用于搜索文本的工具,它根据提供的模式(pattern),在指定文件中搜索匹配该模式的字符串。该命令是Linux系统中用于文本搜索的核心工具之一,具有强大的模式匹配能力。 1. 基本用法: grep ...
grep全称为“global search regular expression(RE) and print out the line”,即全面搜索正则表达式并打印出匹配的行。grep命令的家族还包括egrep和fgrep,其中egrep是对grep的扩展,支持更多的正则表达式元字符,...
Linux grep 命令详解 Linux 中的 grep 命令是一种功能强大的文本搜索工具,通过模式匹配来查找文件中的内容。grep 命令可以根据用户的需求来查找特定的字符串、数字或正则表达式。 grep 命令的基本格式 grep ...
* `reXgrep`:提供 AND、OR、NOT 等语法,还有漂亮的按钮。 * `fungrep`:提供更清楚的输出。 Grep 命令是 Linux 系统中非常有用的命令,通过了解它的使用方法和参数,可以更好地搜索和处理文本文件。
在Linux操作系统中,grep、sed和gawk是三个强大的文本处理工具,它们在日常的系统管理和脚本编写中起着至关重要的作用。了解并熟练掌握这三个工具,能极大地提高工作效率和解决问题的能力。 **grep(Global Regular...
grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。Unix的grep家族包括grep、egrep和fgrep
本篇文章将详细探讨如何通过扩充CMD来实现与Linux命令行的兼容性,特别是针对“grep”这一强大的文本搜索工具。 首先,我们要明白,Linux的命令行工具通常不直接在Windows上运行,因为它们是为类Unix系统设计的。...
Linux 中 grep 命令的使用 grep 命令是 Unix家族中的一种强大的文本搜索工具,能够使用正则表达式搜索文本,并把匹配的行打印出来。grep 命令的全称是 Global Regular Expression Print,表示全局正则表达式版本,...
### Linux grep sed awk命令详解 #### 一、grep命令详解 **grep** 是一个用于查找文件里符合条件的字符串的强大工具。它可以对文件进行快速搜索,找出含有特定模式的行。 - **基本语法**:`grep [选项] 模式 文件...
### Linux中利用grep命令检索文件内容详解 #### 前言 在Linux系统中,进行文件内容搜索是一项常见的任务。为了高效地完成这项任务,Linux提供了多种工具,其中`grep`命令是最为常用的一种。本篇文章将深入探讨如何...
Linux grep 命令 Linux grep 命令用于查找文件里符合条件的字符串。 grep 指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设 grep 指令会把含有范本样式的那一列显示出来...
Linux 下 Grep 命令的使用方法 Grep 命令是 Linux 中一个功能强大且广泛使用的命令,它可以在文件中搜索包含指定模式的行,并将其输出出来。Grep 命令的基本格式为:`grep [选项]... PATTERN [FILE]...` 在使用 ...
【Linux grep命令详解】 在Linux操作系统中,grep命令是一个极其重要的工具,主要用于在文本文件中查找含有特定模式的行。其强大的功能在于它支持正则表达式,这使得grep能够处理复杂的文本匹配需求。grep全称是...