views:

158

answers:

4

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.

+3  A: 

I'd just do it with two greps:

grep "65.55" ex100101.log | grep -v msnbot > results.txt
reko_t
+2  A: 

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.

ghostdog74
A: 

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
Joshua Smith
The LOGFILE needs to be passed to the first grep, not the second.
user9876
+1  A: 

If grep supports lookaheads, you could use

grep "^.*65\.55\.(?:.(?!msnbot))*$" ex100101.log > results.txt 
Jens
This doesn't seem to produce any results in my setup
Nick G