开发者

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

开发者 https://www.devze.com 2022-12-24 14:07 出处:网络
开发者_运维技巧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.
开发者_运维技巧

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
0

精彩评论

暂无评论...
验证码 换一张
取 消