Grep linux примеры 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

Источник

Читайте также:  Эмулятор ключа 1с линукс

How to use logical operators (OR, AND & NOT) with Linux grep command

Linux grep is one of the most used commands by system administrators, computer programmers and ethical hackers. This article will see How to use logical operators (OR, AND, NOT) with Linux grep command. Linux grep command is used to filter information based on pattern matching. Suppose you have logs of the firewall, and you want to search for specific IPs. Linux grep command comes for rescue, and you can use the logical operators’ functionality of this command. Click Here if you are interested in understanding Top Linux Commands for Beginners.

We will use the below data to demonstrate logical AND, OR, and NOT operators in grep:

(1) Use ‘\|’ for using OR in grep

Below commands print lines that match pattern1 or pattern2 till pattern n

(2) Use -E option for using OR in grep

Below commands print lines that match pattern1 or pattern2 till pattern n by using -E option

cat | grep -E '||. '

(3) Use -v option for using the NOT operator in grep

Below commands print lines that not matches pattern1

cat | grep -v ''

(4) AND operator usage example in grep by using multiple patterns

Below commands print lines that matches pattern1, pattern 2 and pattern 3

cat | grep 'pattern1' | grep 'pattern2' | grep 'pattern3'

(5) AND operator implement using -E option

Below commands print lines that match pattern1 and pattern2

Conclusion

Linux grep command is awesome in pattern matching only if you know it very well. In this article, we have covered How to use logical operators (OR, AND & NOT) with Linux grep command.

Читайте также:  Линукс и домен виндовс

Subscribe us to receive more such articles updates in your email.

If you have any questions, feel free to ask in the comments section below. Nothing gives me greater joy than helping my readers!

Disclaimer: This tutorial is for educational purpose only. Individual is solely responsible for any illegal act.

Источник

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