Jeets
Jeets

Reputation: 3367

grep exclude multiple strings

I am trying to see a log file using tail -f and want to exclude all lines containing the following strings:

Nopaging the limit is and keyword to remove is

I am able to exclude one string like this:

tail -f admin.log|grep -v "Nopaging the limit is"

But how do I exclude lines containing either of string1 or string2?

Upvotes: 167

Views: 206243

Answers (8)

Eric Leschinski
Eric Leschinski

Reputation: 153822

Filtering out multiple lines with grep:

Put these lines in filename.txt to test:

abc
def
ghi
jkl

grep command using -E flag with a pipe between tokens in a string:

grep -Ev 'def|jkl' filename.txt

prints:

abc
ghi

egrep using -v flag with pipe between tokens surrounded by parens:

egrep -v '(def|jkl)' filename.txt

prints:

abc
ghi

Or if stacking -e flags through grep parameters is okay (credit -> @Frizlab):

grep -Fv -e def -e jkl filename.txt

prints:

abc
ghi

Upvotes: 169

Yakir GIladi Edry
Yakir GIladi Edry

Reputation: 2821

If you want to use regex:

grep -Ev -e "^1" -e '^lt' -e 'John'

Upvotes: 0

wisbucky
wisbucky

Reputation: 37797

grep -Fv -e 'Nopaging the limit is' -e 'keyword to remove is'

-F matches by literal strings (instead of regex)

-v inverts the match

-e allows for multiple search patterns (all literal and inverted)

Upvotes: 87

Fidel
Fidel

Reputation: 7397

The greps can be chained. For example:

tail -f admin.log | grep -v "Nopaging the limit is" | grep -v "keyword to remove is"

Upvotes: 9

mikhail
mikhail

Reputation: 5169

You can use regular grep like this:

tail -f admin.log | grep -v "Nopaging the limit is\|keyword to remove is"

Upvotes: 15

hs.chandra
hs.chandra

Reputation: 707

tail -f admin.log|grep -v -E '(Nopaging the limit is|keyword to remove is)'

Upvotes: 17

rezizter
rezizter

Reputation: 5158

Another option is to create a exclude list, this is particulary usefull when you have a long list of things to exclude.

vi /root/scripts/exclude_list.txt

Now add what you would like to exclude

Nopaging the limit is
keyword to remove is

Now use grep to remove lines from your file log file and view information not excluded.

grep -v -f /root/scripts/exclude_list.txt /var/log/admin.log

Upvotes: 53

Stefan Podkowinski
Stefan Podkowinski

Reputation: 5249

egrep -v "Nopaging the limit is|keyword to remove is"

Upvotes: 24

Related Questions