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

by Ramesh Natarajan on October 21, 2011

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

Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 28 comments… read them below or add one }

1 Pankaj Kumar October 21, 2011 at 3:07 am

Thanks… it helped me to reduce the length of my script……

2 Ganesh October 21, 2011 at 9:23 am

Today learn “grep ‘pattern1\|pattern2′ filename” patten in grep .. Thank you very much ..

3 xdays October 21, 2011 at 7:36 pm

Great, urged for grep AND&&OR operation

4 jalal hajigholamali October 22, 2011 at 4:28 am

Hi,

Thank you very much..

5 Karthik.P.R October 22, 2011 at 11:29 am

Learned about egrep today.
Thanks !!

இனிய தீபாவளி வாழ்த்துக்கள் ( Happy Diwali – Indian Festival)

6 Jimmy October 22, 2011 at 1:39 pm

Thanks! Good article!

7 Jaymin Patel October 27, 2011 at 12:13 pm

well used AND operations in grep.

8 Curtis November 7, 2011 at 11:33 pm

While not directly related to grep operators, but for for prettier output, use grep –color to print the matched string in colour. Some distros already have this as an alias, if yours does not, you can add it yourself; add the following line to ~/.bashrc :

alias grep=’grep –color’

9 bhargav January 25, 2012 at 1:07 am

It is very nice..
helpful to learn about egrep command

10 Ider April 17, 2012 at 2:59 pm

POSIX standard make makes me painful before, I have to put ‘\’ before ‘|’, ‘{‘, ‘(‘.
-E and -P makes life easier

11 Mac August 20, 2012 at 10:33 pm

Is there anyway to look up or grep for a value in 2 files and if it exists in either or both files then print it but not printing the duplicate llines? Thanks.

12 Curtis August 20, 2012 at 10:46 pm

@Mac:

Not the most elegant way (there is probably a better solution), but anyway:

search=`grep file1`
search+=`grep file2`

echo “$search” | sort | uniq

13 Curtis August 20, 2012 at 11:45 pm

Correction, forgot to include what we’re searching for !

@Mac:

search=`grep value file1`
search+=`grep value file2`

echo “$search” | sort | uniq

14 deerwalker August 23, 2012 at 7:17 am

hello all,

good day,

how to remove lines from a file which contains only “.” or “..” (dot and double dots)?
i.e if a file contains following lines, i want to remove only line 1 and 3.

.
x.txt
..
x..txt
y.txt.file
..y.txt.file

thanks in advance

15 Curtis August 23, 2012 at 10:26 am

Hello @Deerwalker,

If you mean to do this with grep (related to the article), please try:

egrep -v -w “\.|\..” your_file_name

16 succor October 22, 2012 at 4:57 pm

@Curtis & Mac

The search command can be done in one line since grep will accept multiple file names. It is not much, but it is one less line.

search=`grep value file1 file2`

17 Lan January 28, 2013 at 8:39 am

Thanks ^^

18 Özzesh February 6, 2013 at 10:43 pm

Thnx……..It was a real help for me.

19 kkkk March 5, 2013 at 11:35 pm

Thnx… helped a lot but is there any way to grep AND of two patterns only if they are in consecutive lines.. i.e.
pattern1 blah blah blah
pattern2 blan blan blan
pattern3 etc etc etc
pattern1 blah blah blah
pattern3 etc etc etc
pattern2 blan blan blan

i want the output to be only first and second lines where pattern 1 and pattern 2 are consecutive to each other…

thank you

20 Nasdfi April 14, 2013 at 1:09 pm

Thanks!

Also works combined with other command, for example …
ps -A | egrep -i “gnome|kde|mate|cinnamon”
… to know if the desktop environment you use is GNOME, KDE, MATE or Cinnamon.

Useful! Regards

21 DanieW May 14, 2013 at 12:54 pm

Hi kkkk
grep -B 1 pattern2 file1 | grep -A 1 pattern1 -
This will — should if I have it right ;^) — filter first all lines with pattern2 plus its preceding line. Then pipe to filter for all pattern1 with whatever follows (in this case the pattern2).
Otherwise you can head over to awk (gawk)….
A bit late… but still.
Regards

22 Mike May 14, 2013 at 3:17 pm

I have output from the du command ( du -ch / > du ) and am using this pattern:
grep -P “G\t” du

To only show entries with GB. I can use:
grep -P “T\t” du

To show Terabytes. I am trying to see if I can get both using:
grep -P ‘T\t\|G\t’ du

No output. Using:
grep -eP ‘T\t\|G\t’ du

Gives tonnes of output, including lines that should not be matched.

23 Ashurax June 24, 2013 at 1:17 pm

Grep for unique pattern across multiple files.
egrep ‘pattern1|pattern2′ file1 file2 | sort -u

Grep for TB and GB directories in du output.
if using bash:
du -h / | egrep ‘T\t|G\t’

if using zsh:
(Press Control-V and then TAB.)
du -h / | egrep ‘T|G’

24 Aale July 10, 2013 at 10:48 am

Thanks… It was great.

25 sowmya January 2, 2014 at 5:23 am

Thanks….It was really useful.

26 Raghvendra January 10, 2014 at 4:38 am

Thanks for providing us such a well explain commands in details. It will help us to improve skill set and lots of effort in writing scripts.

27 V S Nirvana April 6, 2014 at 9:14 pm

Thanks for providing information for grep command options!!

Have a great time!!

28 Andrew McLauchlan April 15, 2014 at 7:33 pm

Magic, exactly what I was looking for

Leave a Comment

Previous post:

Next post: