0

I am trying to build a grep search that must include the term and exlude another term. I try to use multiple -E "pattern" options but that has not worked.

Here is an example of a command I tried .

grep -Ei "\,17\:31" | grep -Eiv "10\.10\.210\.154" file.csv

but the output results only match the second constraint grep -Eiv "10.10.210.154"

2 Answers2

1

Assuming you want to find \,17\:31 but not from IP 10\.10\.210\.154

You can use this.

grep -Ei "\,17\:31" file.csv | grep -Eiv "10\.10\.210\.154" 
Hemang
  • 186
0

Note: the other answer should work but it explains nothing; hence my explanatory answer.

You told the second grep to read file.csv, so it does. It ignores its standard input because this is what grep does when it operates on file(s).

In effect the second grep acts as if the first one wasn't there. It exits after processing the file and printing its output (if any).

The first grep awaits input (normally from keyboard, if run in an interactive shell). It can take many lines and even print many (matching) lines to its standard output, until the pipe buffer decides to pass the output to the second grep. Then, if the second grep is no more, the first grep is notified the pipe is broken; it exits.

Or you can hit Ctrl+D to indicate end of input; or you can hit Ctrl+C to kill the first grep (and the second, if it's still alive). But this won't fix your data flow.

A proper data flow is like this: you need the first grep to read the file, then pipe the result to the second one:

grep -Ei "\,17\:31" file.csv | grep -Eiv "10\.10\.210\.154"
# or
<file.csv grep -Ei "\,17\:31" | grep -Eiv "10\.10\.210\.154"

The latter syntax has at least one advantage, explained at the end of this another answer of mine.