Linux bash grep and

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

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

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’

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

6. Grep AND using Multiple grep command

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 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

Источник

Читайте также:  Самые лучшие дистрибутивы линукса

Grep OR – Grep AND – Grep NOT – Match Multiple Patterns

The grep , egrep , sed and awk are the most common Linux command line tools for parsing files.

From the following article you’ll learn how to match multiple patterns with the OR , AND , NOT operators, using grep , egrep , sed and awk commands from the Linux command line.

I’ll show the examples of how to find the lines, that match any of multiple patterns, how to print the lines of a file, that match each of provided patterns and how to find and print the lines, that do not match a pattern (negative matching).

Cool Tip: Find and validate email addresses with grep command! The best regular expression for email addresses! Read more →

GREP OR: Match Any Of Multiple Patterns

Find all the lines of a file, that match any of provided patterns.

Using grep and egrep commands:

$ grep "PATTERN1\|PATTERN2" FILE $ grep -E "PATTERN1|PATTERN2" FILE $ grep -e PATTERN1 -e PATTERN2 FILE $ egrep "PATTERN1|PATTERN2" FILE
$ awk '/PATTERN1|PATTERN2/' FILE
$ sed -e '/PATTERN1/b' -e '/PATTERN2/b' -e d FILE

GREP AND: Match Multiple Patterns

It is also often required to grep a file for multiple patterns – when it is needed to find all the lines in a file, that contain not one, but several patterns.

Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order.

Use one of the following commands to find and print all the lines of a file, that match multiple patterns.

Using grep command (exact order):

$ grep -E 'PATTERN1.*PATTERN2' FILE

Using grep command (any order):

$ grep -E 'PATTERN1.*PATTERN2|PATTERN2.*PATTERN1' FILE $ grep 'PATTERN1' FILE | grep 'PATTERN2'

Cool Tip: The server is out of memory? Check what processes are using all the RAM and SWAP! Bash one liner for the true Linux admins! Read more →

Читайте также:  Network is unreachable linux virtualbox

Using awk command (exact order):

$ awk '/PATTERN1.*PATTERN2/' FILE

Using awk command (any order):

$ awk '/PATTERN1/ && /PATTERN2/' FILE

Using sed command (exact order):

$ sed '/PATTERN1.*PATTERN2/!d' FILE

Using sed command (any order):

$ sed '/PATTERN1/!d; /PATTERN2/!d' FILE

GREP NOT: Negative Matching

Cool Tip: Find and validate IP addresses with grep command! The best regular expression for IP addresses! Read more →

Find and print all the lines, that do not match a pattern.

Источник

Оцените статью
Adblock
detector