Linux grep and or command

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

Источник

Читайте также:  Linux user password script

Grep OR – Grep AND – Grep NOT – Match Multiple Patterns

The grep , egrep , sed and awk are the most common Linux command line tools for parsing files.

From the following article you’ll learn how to match multiple patterns with the OR , AND , NOT operators, using grep , egrep , sed and awk commands from the Linux command line.

I’ll show the examples of how to find the lines, that match any of multiple patterns, how to print the lines of a file, that match each of provided patterns and how to find and print the lines, that do not match a pattern (negative matching).

Cool Tip: Find and validate email addresses with grep command! The best regular expression for email addresses! Read more →

GREP OR: Match Any Of Multiple Patterns

Find all the lines of a file, that match any of provided patterns.

Using grep and egrep commands:

$ grep "PATTERN1\|PATTERN2" FILE $ grep -E "PATTERN1|PATTERN2" FILE $ grep -e PATTERN1 -e PATTERN2 FILE $ egrep "PATTERN1|PATTERN2" FILE
$ awk '/PATTERN1|PATTERN2/' FILE
$ sed -e '/PATTERN1/b' -e '/PATTERN2/b' -e d FILE

GREP AND: Match Multiple Patterns

It is also often required to grep a file for multiple patterns – when it is needed to find all the lines in a file, that contain not one, but several patterns.

Note, that you can both find the lines in a file that match multiple patterns in the exact order or in the any order.

Use one of the following commands to find and print all the lines of a file, that match multiple patterns.

Using grep command (exact order):

$ grep -E 'PATTERN1.*PATTERN2' FILE

Using grep command (any order):

$ grep -E 'PATTERN1.*PATTERN2|PATTERN2.*PATTERN1' FILE $ grep 'PATTERN1' FILE | grep 'PATTERN2'

Cool Tip: The server is out of memory? Check what processes are using all the RAM and SWAP! Bash one liner for the true Linux admins! Read more →

Using awk command (exact order):

$ awk '/PATTERN1.*PATTERN2/' FILE

Using awk command (any order):

$ awk '/PATTERN1/ && /PATTERN2/' FILE

Using sed command (exact order):

$ sed '/PATTERN1.*PATTERN2/!d' FILE

Using sed command (any order):

$ sed '/PATTERN1/!d; /PATTERN2/!d' FILE

GREP NOT: Negative Matching

Cool Tip: Find and validate IP addresses with grep command! The best regular expression for IP addresses! Read more →

Читайте также:  Linux user password script

Find and print all the lines, that do not match a pattern.

Источник

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:

Читайте также:  Linux tftp client get file

(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:

Источник

Linux Grep OR, AND, NOT Operator and Logic Examples

lo grep is very useful tool for text search and pattern matching. We have all ready provided tutorial and examples about grep and egrep . In this tutorial we will look grep command or , and , not logic operations in detail.

Example Text

We will use following text during tutorial for grep operations. This is the wage list of Manchester United Football Team.

David de Gea 26 £200,000 2 Years (2019) Sergio Romero 30 £50,000 4 Years (2021) Marcos Rojo 27 £70,000 2 Years (2019) Phil Jones 25 £50,000 2 Years (2019) Chris Smalling 27 £80,000 2 Years (2019) Eric Bailly 23 £75,000 3 Years (2020)

OR Logic

or logic matches all lines if one of the pattern match. We will use \| to specify OR logic. In this example we will look players those age 23 OR 30 .

OR Logic

OR with Extended Grep

Another way to implement OR logic is using grep command extended option with -E . We will specify OR logic with | . In this example we will look players those age 23 OR 30 .

$ grep -E "23|30" manchester.txt

OR with Extended Grep

OR with Egrep

Another tool used to implement OR logic is egrep . We will use | operator again. In this example we will look players those age 23 OR 30 .

OR with Egrep

AND Logic

AND logic will match lines those have all provided patterns. We will use .* for AND operator. In this example we will list players those contract is 2 years and age is 27.

$ grep "27.*2 Years" manchester.txt

AND Logic

AND with Multiple Grep

Another implementation is using multiple grep commands to filter given patterns. In this example we will list players those contract is 2 years and age is 27.

$ grep "2 Years" manchester.txt | grep 27

AND with Multiple Grep

NOT Logic

NOT logic is used to get results those do not matched given pattern. We will use -v option for grep. In this example we will list players those do not 27 years old.

NOT Logic

NOT with Multiple Grep Conditions

We can implement NOT logic multiple times with multiple grep commands. We will pipe multiple grep commands in bash. In this example we will list players those is not 27 years old and not have 2 Years contract.

$ grep -v "27" manchester.txt | grep -v "2 Years"

Источник

Оцените статью
Adblock
detector