Novice User
Novice User

Reputation: 3824

Grep for matching pattern but exclude particular string

I've file containing various log lines, and I want to grep for a pattern but exclude when message:com.mycompany.excluded , so basically following should be returned :

"The log found this message blah, message:com.blahblah"
"The log found this message blah2, message:com.blahblah2"
"The log found this message foobar, message:com.mycompany"
"The log found this message blah, message:com.mycompany.included"

but not :

 "The log found this message blah, message:com.mycompany.excluded"

I'm using this pattern, but it would not work for excluding com.mycompany.excluded

grep "The log found this message.*message:.*" "mylogs.txt"

Upvotes: 0

Views: 64

Answers (2)

Bill Horvath
Bill Horvath

Reputation: 1717

What you want here is sed, not grep:

sed -i .bak 's/^.*excluded//' mylogs.txt

(The -i .bak creates a backup of your original file with the extension .bak; this would leave you with mylogs.txt and mylogs.txt.bak, with mylogs.txt having the undesired lines removed.)

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203129

awk '/The log found this message.*message:/ && !/com.mycompany.excluded/' "mylogs.txt"

There's various ways to make it more robust but they're probably not necessary (depending on what the rest of your log file looks like).

Upvotes: 1

Related Questions