Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invert a grep expression

Tags:

regex

linux

grep

The following grep expression successfully lists all the .exe and .html files in the current directory and sub directories.

ls -R |grep -E .*[\.exe]$\|.*[\.html]$   

How do I invert this result to list those that aren't a .html or .exe instead. (That is, !=.)

like image 433
sMaN Avatar asked Dec 07 '10 05:12

sMaN


People also ask

How do you invert grep?

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.

How do you grep with regular expressions?

Grep Regular Expression In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

How do I grep exact string in Linux?

The easiest of the two commands is to use grep's -w option. This will find only lines that contain your target word as a complete word. Run the command "grep -w hub" against your target file and you will only see lines that contain the word "hub" as a complete word.


2 Answers

Use command-line option -v or --invert-match,

ls -R |grep -v -E .*[\.exe]$\|.*[\.html]$ 
like image 133
Eric Fortis Avatar answered Sep 23 '22 05:09

Eric Fortis


grep -v 

or

grep --invert-match 

You can also do the same thing using find:

find . -type f \( -iname "*" ! -iname ".exe" ! -iname ".html"\) 

More info here.

like image 43
darioo Avatar answered Sep 21 '22 05:09

darioo