Grep not match linux

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

Источник

Читайте также:  Postgresql linux default password

Negative matching using grep (match lines that do not contain foo)

[^error_log] would never ever work anyway, [] are char classes, regexp ‘s in general are not good at negative patterns (unless the engine implements negative lookaheads).

3 Answers 3

-v, —invert-match select non-matching lines

Also check out the related -L (the complement of -l ).

-L, —files-without-match only print FILE names containing no match

Worth mentioning that for multiple (negative) matches -e option can be used: grep -v -e ‘negphrase1’ -e ‘negphrase2’

Similar to the comment from @Babken-Vardanyan Also — able to use pipes to join multiple matches e.g. grep -v ‘negphrase1|negphrase2|negphrase3’

Last comment is NOT the same as it will search for things that don’t match both rather than either. ie If it matches one but not the other its still printed. Try it both ways with non-similar strings

@EvanLanglois — forcing grep to interpret the pattern as an extended regular expression using -E works, i.e. grep -vE ‘negphrase1|negphrase2|negphrase3’

@OlleHärstedt, I think I misunderstood your scenario in my previous comment, the following may be what you’re looking for grep «» /dev/null * | grep foo | grep -v bar | cut -d: -f1 | sort -u (why the first grep?, there’s always a way :))

You can also use awk for these purposes, since it allows you to perform more complex checks in a clearer way:

Lines containing neither foo nor bar :

Lines containing neither foo nor bar which contain either foo2 or bar2 :

That’s actually quite cool. You don’t even have to learn the complete awk language in order to group regexp with logical operators. Thanks for this answer!

Читайте также:  Сервер установить время linux

It’s possible that the OP wasn’t aware of awk capabilities and providing an alternate solution that provides the same result is helpful.

In your case, you presumably don’t want to use grep, but add instead a negative clause to the find command, e.g.

find /home/baumerf/public_html/ -mmin -60 -not -name error_log 

If you want to include wildcards in the name, you’ll have to escape them, e.g. to exclude files with suffix .log:

find /home/baumerf/public_html/ -mmin -60 -not -name \*.log 

Источник

How To Use Negative Matching With grep In Linux (Print Lines That Don’t Match)

Anthony Heddings

Anthony Heddings
Writer

Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon’s AWS platform. He’s written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read more.

Bash Shell

grep is a command line search utility for Linux that will print out lines in files that match a pattern or regular expression. It’s also useful to invert matches, which will filter out all lines in a file that contain the given string.

Negative Matching With grep

To use negative matching in grep , you should execute the command with the -v or —invert-match flags. This will print only the lines that don’t match the pattern given.

Keep in mind though that since grep isn’t matching anything, there’s no way to use the -o flag to print “only the matches,” since nothing has technically matched. grep is able to match multiple times per line, but in this case, it will not matter if there are multiple matches. Any single match will cause grep to omit the line.

This works with regular expressions (regex) as well and will print any line that files to match the regex.

While you can write regular expressions that do negative matching, it’s generally easier to invert the match this way.

Negative Matching Filenames

The lowercase -l flag will cause grep to print the filenames containing matches instead of the actual matched content. This can be useful when scanning a list of files, but similarly, it’s also useful to invert this sometimes. The inverse of -l is the uppercase variant, -L , which does the opposite:

Читайте также:  Daemon tool lite linux

Make sure you don’t use the -v flag with -L to invert the match before printing the files without the match, or you will run into a “double opposites” situation where the match is inverted twice and does not have the intended effect.

Profile Photo for Anthony Heddings

Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon’s AWS platform. He’s written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times.
Read Full Bio »

Источник

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