- 7 Linux Grep OR, Grep AND, Grep NOT Operator Examples
- Grep OR Operator
- 1. Grep OR Using \|
- 2. Grep OR Using -E
- 3. Grep OR Using egrep
- 4. Grep OR Using grep -e
- Grep AND
- 5. Grep AND using -E ‘pattern1.*pattern2’
- 6. Grep AND using Multiple grep command
- Grep NOT
- 7. Grep NOT using grep -v
- How to use logical operators (OR, AND & NOT) with Linux grep command
- (1) Use ‘\|’ for using OR in grep
- (2) Use -E option for using OR in grep
- (3) Use -v option for using the NOT operator in grep
- (4) AND operator usage example in grep by using multiple patterns
- (5) AND operator implement using -E option
- Conclusion
- Related
- Grep: how to add an «OR» condition? [duplicate]
- 5 Answers 5
- Grep “OR” Condition Tutorial
- OR Condition without Option
- OR Condition with -E Option
- OR Condition with -e Options
- egrep Command OR Condition
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
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.
Related
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.
Grep: how to add an «OR» condition? [duplicate]
How can I introduce a conditional OR into grep? Something like, grepping a file’s type for (JPEG OR JPG), and then sending only those files into the photos folder. For example. I know how to send the file where I want it, and get the file type, I just need some help with the grep part. I’m on OS X, which IMO seems to have modified/customized *nix utilities than what I’m used to in a *nix environment. So hopefully the answers can be as generic/portable as possible.
One might argue that OSX has the more *nix binaries since it is mostly BSD based. I suspect you are far more used the gnu tools.
@Zoredache Uhh .. sure! What you said! I am most familiar with Debian, though I have dabbled with Slackware, CentOS, Gentoo, Ubuntu, and a few others. Debian is my favourite, I almost can’t stand Gentoo and Ubuntu, and Slackware I find to be a bit too unfriendly for me. I tend to break stuff. Great way to learn, though.
You don’t need regexes if you just want to grep for different fixed patterns. Just use the -e parameter for each pattern you want to match, or one -F if you want to supply them as a newline separated list, or -f if you want to read them from a file (see man grep ).
5 Answers 5
You can pass multiple regexes to grep by using the -e option more than once:
grep -e regex1 -e regex2 input_file
will match lines matching regex1 or regex2 .
grep -E «ATTN|MASS|BOND|ANGLE|DIHE|IMPROPER» temp.dat and grep -e ATTN -e MASS -e BOND -e ANGLE -e DIHE -e IMPROPER temp.dat are the same.
I’m also fairly new to regex, but since noone else has answered I’ll give it a shot. The pipe-operator «|» is used for an OR operator.
The following REGEX should get you going somewhere.
(Match anything one or more times followed by either «JPG» or «JPEG» at the end)
Extended answer (editted after learning a bit about (e)grep): Assuming you have a folder with following files in them:
test.jpeg, test.JpEg, test.JPEG, test.jpg, test.JPG, test.notimagefile, test.gif
(Not that creative with names. )
First we start by defining what we know about our pattern: We know that we are looking for the end of the name. Ergo we use the «$» operand to define that each line has to end with the defined pattern. We know that the pattern needs to be either JPEG or JPG. To this we use the pipeline «|» as an or operand. Our pattern is now:
(Match any line ending with EITHER «JPEG» or «JPG»)
However we see that in this example, the only difference is the optional «E». To this we can use the «?» operand (meaning optional). We write:
(Mach any file ending with a pattern like: «J», followed by «P», followed by an optional «E», followed by a «G»).
However we might also like to match files with lowercase letters in file name. To this we introduce the character-class «[. ]». meaning match either of the following. We write:
(Match any file ending with at pattern like: «j» or «J», followed by «p» or «P», followed by an optional «e» or «E», followed by «g» or «G») (This could also be done using the «-i» option in grep, but I took this as an exercise in REGEX)
Finally, since we (hopefully) start to see a pattern, we can omit the unnecessary parentheses. Since there is only one optional letter («E»), we can omit this one. Also, since the file only has this pattern to end on, we can omit the starting and ending parenthesis. Thus we simply get:
Finally; lets assume you also want to find files with «.gif»-filetype, we can add this as a second parameter:
(Here I’ve again added extra parenthesis for readability/grouping. Feel free to remove them if they seem obfuscating.)
Finally, I used ls and a pipeline to send all file names to (e)grep:
test.gif test.JPG test.JpEg test.JPEG test.jpg test.JPG
Second edit: Using the -i option and omitting parenthesis we can shorten it down to only:
Grep “OR” Condition Tutorial
The grep command is used to filter or search or match some text or regex in a text file. The grep command provides a lot of different options and features that can be used for different cases. The grep command can be used to match one of the multiple search terms which can be accomplished with the OR logic. The OR logic is used to match one of the provided search terms. In this tutorial, we examine how to use the OR condition.
OR Condition without Option
The most basic implementation for the grep OR condition is using the \| characters like below.
grep "PATTERN1\|PATTERN2\|PATTERN3" FILE
- PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
- \| characters are used to make OR condition and put between search terms or patterns.
- FILE is the file we search.
In the following example, we match one of “ferrari”,”opel”,”audi”.
$ grep "ferrari\|opel\|audi" cars.txt
OR Condition with -E Option
The grep command OR condition can be enabled with the -E option like below.
grep -E "PATTERN1|PATTERN2|PATTERN3" FILE
- PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
- -E option is used to make search term with OR condition.
- | character is used for OR condition
In the following example, we match one of “ferrari”,”opel”,”audi”.
$ grep -E "ferrari|opel|audi" cars.txt
OR Condition with -e Options
Another way to create OR condition with grep command is using the -e option. For every OR condition the -e option is used once and the pattern or search term is added.
grep -e PATTERN1 -e PATTERN2 -e PATTERN3 FILE
- PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
- -e option is used to specify every condition. The -e option is used multiple times to specify multiple OR conditions.
In the following example, we match one of “ferrari”,”opel”,”audi”.
$ grep -e "ferrari" -e "opel" -e "audi" cars.txt
egrep Command OR Condition
The egrep command is a bit extended implementation of the grep command. The egrep command is the same as “grep -E”.
egrep "PATTERN1|PATTERN2|PATTERN3" FILE
- egrep command is extended version of the grep command and provides the -e and -E options automatically by default.
- PATTERN1,PATTERN2 and PATTERN3 are the search terms or patterns we want to match.
- | character is used to implement OR conditions.
In the following example, we match one of “ferrari”,”opel”,”audi”.
$ grep -E "ferrari|opel|audi" cars.txt