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






My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 22 comments… read them below or add one }
Thanks… it helped me to reduce the length of my script……
Today learn “grep ‘pattern1\|pattern2′ filename” patten in grep .. Thank you very much ..
Great, urged for grep AND&&OR operation
Hi,
Thank you very much..
Learned about egrep today.
Thanks !!
இனிய தீபாவளி வாழ்த்துக்கள் ( Happy Diwali – Indian Festival)
Thanks! Good article!
well used AND operations in grep.
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’
It is very nice..
helpful to learn about egrep command
POSIX standard make makes me painful before, I have to put ‘\’ before ‘|’, ‘{‘, ‘(‘.
-E and -P makes life easier
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.
@Mac:
Not the most elegant way (there is probably a better solution), but anyway:
search=`grep file1`
search+=`grep file2`
echo “$search” | sort | uniq
Correction, forgot to include what we’re searching for !
@Mac:
search=`grep value file1`
search+=`grep value file2`
echo “$search” | sort | uniq
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
Hello @Deerwalker,
If you mean to do this with grep (related to the article), please try:
egrep -v -w “\.|\..” your_file_name
@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`
Thanks ^^
Thnx……..It was a real help for me.
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
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
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
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.