NickG
NickG

Reputation: 9820

Regular expression incorporating an exclusion (ie "does not contain") for grep

I'm trying to filter webserver log files using grep. I need to output all lines containing 65.55. but exclude those matching lines which contain msnbot.

My starting point is this - but it doesn't work:

grep "^.*65\.55\..*(!msnbot).*$" ex100101.log > results.txt

I'm using grep for Windows (hence double quotes) but I doubt this matters.

Upvotes: 3

Views: 2034

Answers (4)

Jens
Jens

Reputation: 25573

If grep supports lookaheads, you could use

grep "^.*65\.55\.(?:.(?!msnbot))*$" ex100101.log > results.txt 

Upvotes: 1

ghostdog74
ghostdog74

Reputation: 342649

normally, you use 2 greps. one to grep the pattern you want, the other with -v option to exclude the pattern. however you can use awk, which does it all in one process.

awk '/.*65\.55.*/ && !/msnbot/' ext100101.log >results.txt

you can download awk for windows here.

Upvotes: 2

Joshua Smith
Joshua Smith

Reputation: 6631

The easiest thing is to pipe the output of the first command and exclude lines using grep -v

grep FINDPATTERN | grep -v EXCLUDEPATTERN LOGFILE > results.txt

Upvotes: 0

reko_t
reko_t

Reputation: 56440

I'd just do it with two greps:

grep "65.55" ex100101.log | grep -v msnbot > results.txt

Upvotes: 5

Related Questions