A regular expression that includes an exception (that is, "Does not contain") for grep

I am trying to filter web server log files using grep. I need to print all lines containing 65.55. but exclude matching lines containing msnbot .

My starting point is this, but it does not work:

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

I use grep for Windows (hence double quotes), but I doubt it matters.

+3
source share
4 answers

I would just do it with two greps:

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

2 greps. grep , , -v . awk, .

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

awk windows .

+2

If grep supports lookaheads, you can use

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

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

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

Source: https://habr.com/ru/post/1738651/


All Articles