≡ Menu

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

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
Add your comment

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

Comments on this entry are closed.

  • Pankaj Kumar October 21, 2011, 3:07 am

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

  • Ganesh October 21, 2011, 9:23 am

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

  • xdays October 21, 2011, 7:36 pm

    Great, urged for grep AND&&OR operation

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

    Hi,

    Thank you very much..

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

    Learned about egrep today.
    Thanks !!

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

  • Jimmy October 22, 2011, 1:39 pm

    Thanks! Good article!

  • Jaymin Patel October 27, 2011, 12:13 pm

    well used AND operations in grep.

  • Curtis November 7, 2011, 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’

  • bhargav January 25, 2012, 1:07 am

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

  • Ider April 17, 2012, 2:59 pm

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

  • Mac August 20, 2012, 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.

  • Curtis August 20, 2012, 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

  • Curtis August 20, 2012, 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

  • deerwalker August 23, 2012, 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

  • Curtis August 23, 2012, 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

  • succor October 22, 2012, 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`

  • Lan January 28, 2013, 8:39 am

    Thanks ^^

  • Özzesh February 6, 2013, 10:43 pm

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

  • kkkk March 5, 2013, 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

  • Nasdfi April 14, 2013, 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

  • DanieW May 14, 2013, 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

  • Mike May 14, 2013, 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.

  • Ashurax June 24, 2013, 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’

  • Aale July 10, 2013, 10:48 am

    Thanks… It was great.

  • sowmya January 2, 2014, 5:23 am

    Thanks….It was really useful.

  • Raghvendra January 10, 2014, 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.

  • V S Nirvana April 6, 2014, 9:14 pm

    Thanks for providing information for grep command options!!

    Have a great time!!

  • Andrew McLauchlan April 15, 2014, 7:33 pm

    Magic, exactly what I was looking for

  • Durai May 15, 2014, 1:49 am

    Hi,

    I am using the below command to get the count, which are all the files having documentShare.*cGRm. I want to get the file name also. Can you please guide me how to get that too in the same command.

    grep -rn ‘documentShare.*cGRm’ var/log/ejabberd/muclogs/ |wc -l

  • thanks June 3, 2014, 5:41 pm

    thanks – it helped me to do some stuff – thank you 🙂

  • Jams December 6, 2014, 10:03 pm

    How to use grep where i need to search two strings from two different lines.

  • DanieW December 9, 2014, 2:15 am

    Jams … This seems to work fine for my directory listing
    ls | egrep ‘\-mon|x VMs’
    temper-mon.pl
    VirtualBox VMs
    What is the problem?

  • Rafael January 5, 2015, 7:35 am

    Thx Ramesh,

    just found the answer for a long lasting question. Thx, you saved my day!!

    bye,
    Rafael

  • XYZ January 15, 2015, 2:27 pm

    I want to search files which have pattern1 in one line and pattern2 in another line

    So my requirement is pattern1 AND pattern2 but they will not be in same line inside the file. – How to do in grep or anyother command is fine.

  • Andrew Kh January 29, 2015, 11:56 pm

    Hi All.
    I need to take a list of files which contain ‘pattern1’ AND not contain ‘pattern2’. How can I do this?

  • Rahim February 27, 2015, 10:49 am

    I am looking for a multiple grep with NOT and AND conditions. I have a directory with with txt files and some csv files which have the date included in the filename. I want to delete the csv files that do not include today’s date. So I am trying the code below in bash

    #!/bin/bash
    now=$(date “+%m-%d-%Y”)
    dir=”/var/tmp/”
    for f in “$dir”/*; do
    ls -1 | grep -v *$now* | grep *csv* | xargs rm -f
    done

    This is not deleting anything. If I take out the grep *csv* operator then it deletes the text files. Only the CSV files have dates on them, the text files don’t. Please advise.

  • Vivek July 17, 2015, 2:49 pm

    Excellent tutorial!! Thanks a lot!

  • Felipe July 21, 2015, 8:57 am

    Thanks!, I was looking how to do an OR, this was very helpful.

  • mandeep August 17, 2015, 12:48 am

    Good Article…

  • Anonymous October 2, 2015, 4:56 am

    Thanks for the excellent article.

    Rahim!
    It is easier to use find for that:
    find /var/tmp/ -name *.csv -mtime +1 -exec rm -f ‘{}’ \;
    -mtime operates with days, if that’s too long, experiment with -mmin +n, which finds files which were modified longer than n minutes
    Good luck!

  • shgy October 5, 2015, 10:17 am

    useful! Thanks!!! 🙂

  • Manoj December 2, 2015, 10:04 am

    How can i grep a word for starting from 15 to30 files (not lines) . say file is ending with following date format yyyy-mm-dd.

  • Jon Neville February 10, 2016, 11:12 am

    If a pattern contains spaces, should that pattern be in double quotes inside the single quotes? That did not work for me. Should the other pattern be put in double quotes even if it is a single word?

    grep -H -r -E ‘”for example”.*sample_2|sample_2.*”for example”‘ *

  • tot-to February 12, 2016, 11:28 am

    egrep ‘Manager|Developer’ employee.txt | grep -v Sales

    This is the combination of NOT and AND. How to make a combination of NOT and OR? I.e. I wanna exclude from the initial list only people who’s name is John and who are not managers. Johns who are managers and people with other names should stay in the grepped list.