Question

I need a way to simplify this command:

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

It should find all the lines including SEARCHTERM but exclude if one of the SEARCHTERM lines includes PHHIABFFH or Stats.

Was it helpful?

Solution

This will work:

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

OTHER TIPS

Why do you want to 'simplify' this pipe? Un*x commandline tools are made to be chained in a way like this.

Edit

Some of answers suggest using features of specific versions of grep. I like this but it is very possible that such specific features are not present in the version of grep used by the OP.

So as long as we don't know what environment the OP has and what version of which tool he can use I suggest to stick to the common features present in all versions of the tools used.

This again nicely illustrates the Un*x philosophy.

It's easy to underestimate the power of combining pipes and redirection. As an instructive example, The Unix Shell As a 4GL [Schaffer-Wolf] shows that with these facilities as a framework, a handful of simple utilities can be combined to support creating and manipulating relational databases expressed as simple textual tables.

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

If simple means just one command with more args ...

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

you can just use the shell

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

I would have just done this:

grep 'SEARCHTERM' server.log | grep -v -e 'PHHIABFFH' -e 'Stats'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top