English 中文(简体)
GREP: How to search for a value but at the same time exclude some matches
原标题:

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.

最佳回答

This will work:

grep  SEARCHTERM  server.log | grep -Ev  PHHIABFFH|Stats 
问题回答

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 




相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...

热门标签