GREP: how to search for a value, but at the same time exclude some matches

I need to simplify this command:

grep 'SEARCHTERM' server.log | grep -v 'PHHIABFFH' | grep -v 'Stats'

It should find all lines, including SEARCHTERM, but exclude if one of the lines SEARCHTERMincludes PHHIABFFHor Stats.

+3
source share
5 answers

This will work:

grep 'SEARCHTERM' server.log | grep -Ev 'PHHIABFFH|Stats'
+5
source

Why do you want to "simplify" this channel? Un * x command line tools are designed to be chained this way.

Edit

Some answers suggest using the functions of certain versions of grep. I like it, but it is very possible that such specific functions are not available in the grep version used by the OP.

, , OP, , , .

Un * x.

. , Unix Shell 4GL [Schaffer-Wolf] , , .

http://www.catb.org/~esr/writings/taoup/html/ch07s02.html#plumbing

+6

...

awk '$0 !~ /PHHIABFFH|Stats/ && /SEARCHTERM/' server.log
+1

while read -r line
do
   case "$line" in
    *"Stats"*|*"PHHIABFFH"*) continue;;
    *"SEARCHTERM"* ) echo "$line";;
   esac
done < "file"
0

:

grep 'SEARCHTERM' server.log | grep -v -e 'PHHIABFFH' -e 'Stats'
0
source

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


All Articles