brief introduction
In grep, we have options equivalent to OR and NOT operators, but no AND operators. However, we can use patterns to simulate AND. The examples in this article will help to understand how to use OR, AND and NOT operations of grep commands for text search.
This article will use the following employee.txt file as an example to illustrate:
$ 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
Grep OR operator
The grep OR function can be implemented using the four methods provided below.
1. Use "\"
If you use the grep command without any options, you need to use "\" to separate multiple modes of conditional OR.
grep 'pattern1\|pattern2' filename
For example, grep comes from Tech or Sales in the employee.txt file. If there is no backslash before the separator |, the following operation is invalid.
[jinguang1@localhost ~]$ grep "Tech|Sales" employee.txt [jinguang1@localhost ~]$ 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. Use the - E option
The grep-E option is used to extend regular expressions. If you use the grep command with the - E option, you only need to use | as the conditional OR to separate multiple modes.
grep -E 'pattern1|pattern2' filename
For example, grep comes from Tech or Sales in the employee.txt file. Use only "|" as multiple patterns.
[jinguang1@localhost ~]$ 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. Use egrep
The functions of egrep and grep-E are identical.
egrep 'pattern1|pattern2' filename
For example, grep comes from Tech or Sales in the employee.txt file. Use only "|" as multiple patterns.
[jinguang1@localhost ~]$ 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. Use grep-e
With the grep-e option, you can only use one parameter. If you want to use multiple modes, you need to provide multiple - e options in the command.
grep -e pattern1 -e pattern2 filename
For example, grep comes from Tech or Sales in the employee.txt file. Use multiple - e options to support multiple OR modes.
[jinguang1@localhost ~]$ 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
1. Use - E "pattern1.*pattern2"
There is no AND operator in grep, but you can use the - E option to simulate it.
grep -E 'pattern1.*pattern2' filename grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename
The following example will grep all rows containing "Dev" and "Tech" at the same time, and keep the corresponding sequence containing strings.
[jinguang1@localhost ~]$ grep -E "Dev.*Tech" employee.txt 200 Jason Developer Technology $5,500
The following example will grep all rows containing "Manager" and "Sales" and the string can appear in any order.
[jinguang1@localhost ~]$ grep -E "Manager.*Sales|Sales.*Manager" employee.txt 100 Thomas Manager Sales $5,000 500 Randy Manager Sales $6,000
2. Use multiple grep commands
You can use pipes to connect multiple grep commands to simulate the AND operator.
grep -E 'pattern1' filename | grep -E 'pattern2'
The following example will grep all rows containing "Manager" and "Sales" and the string can appear in any order.
[jinguang1@localhost ~]$ grep Manager employee.txt | grep Sales 100 Thomas Manager Sales $5,000 500 Randy Manager Sales $6,000
Grep NOT
Using the grep-v option, you can simulate the NOT operator. The - V option is designed to match all rows that do not contain the specified pattern.
grep -v 'pattern1' filename
The following example shows all rows that do not contain the "Sales" keyword.
[jinguang1@localhost ~]$ grep -v Sales employee.txt 200 Jason Developer Technology $5,500 300 Raj Sysadmin Technology $7,000 400 Nisha Manager Marketing $9,500
The following example shows all Manager s and Developer s, but ignores Sales.
[jinguang1@localhost ~]$ grep -E "Manager|Developer" employee.txt | grep -v Sales 200 Jason Developer Technology $5,500 400 Nisha Manager Marketing $9,500