How to invert a grep expression

RegexLinuxGrep

Regex Problem Overview


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, !=.)

Regex Solutions


Solution 1 - Regex

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

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

Solution 2 - Regex

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.

Solution 3 - Regex

Add the -v option to your grep command to invert the results.

Solution 4 - Regex

As stated multiple times, inversion is achieved by the -v option to grep. Let me add the (hopefully amusing) note that you could have figured this out yourself by grepping through the grep help text:

grep --help | grep invert

> -v, --invert-match select non-matching lines

Solution 5 - Regex

 grep "subscription" | grep -v "spec"  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsMaNView Question on Stackoverflow
Solution 1 - RegexEric FortisView Answer on Stackoverflow
Solution 2 - RegexdariooView Answer on Stackoverflow
Solution 3 - RegexRob SobersView Answer on Stackoverflow
Solution 4 - Regexjmd_dkView Answer on Stackoverflow
Solution 5 - RegexAnja IshmukhametovaView Answer on Stackoverflow