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.
I'd just do it with two greps:
grep "65.55" ex100101.log | grep -v msnbot > results.txt
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.
If grep supports lookaheads, you could use
grep "^.*65\.55\.(?:.(?!msnbot))*$" ex100101.log > results.txt
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
精彩评论