Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Invert search using grep for multiple strings of text

I am trying filter out couple of blocks of text that keep repeating over and over in my log file. For eg;

grep -v ("string one that I don't want" \| "string two that I don't want") file.log

I tried several variations of this and tried tweaking the white spaces . Sometimes it will filter out the first string sometimes neither. What will be the correct format to filter out more than one block of text using grep ?

like image 448
user2476714 Avatar asked Dec 15 '13 05:12

user2476714


People also ask

How do I reverse grep search?

In order to invert a grep expression, we just need to make use of the -v flag along with the grep command.

What is the option given to grep to invert the matching?

To invert the Grep output , use the -v flag. The -v option instructs grep to print all lines that do not contain or match the expression. The –v option tells grep to invert its output, meaning that instead of printing matching lines, do the opposite and print all of the lines that don't match the expression.

Can grep command be used for searching a pattern in more than one file?

grep command can be used for searching a pattern in more than one file.


1 Answers

You can use -e option multiple times in grep to skip multiple search items:

grep -v -e "string one that I don't want" -e "string two that I don't want" file.log

OR else use regex using grep -E for extended regex support:

grep -vE 'string one|string two' file.log
like image 199
anubhava Avatar answered Oct 18 '22 21:10

anubhava