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
Comments on this entry are closed.
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.
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’
Thanks… It was great.
Thanks….It was really useful.
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.
Thanks for providing information for grep command options!!
Have a great time!!
Magic, exactly what I was looking for
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 – it helped me to do some stuff – thank you 🙂
How to use grep where i need to search two strings from two different lines.
Jams … This seems to work fine for my directory listing
ls | egrep ‘\-mon|x VMs’
temper-mon.pl
VirtualBox VMs
What is the problem?
Thx Ramesh,
just found the answer for a long lasting question. Thx, you saved my day!!
bye,
Rafael
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.
Hi All.
I need to take a list of files which contain ‘pattern1’ AND not contain ‘pattern2’. How can I do this?
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.
Excellent tutorial!! Thanks a lot!
Thanks!, I was looking how to do an OR, this was very helpful.
Good Article…
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!
useful! Thanks!!! 🙂
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.
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”‘ *
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.