Monday, October 10, 2022

[SOLVED] Search files for a string but exclude another string

Issue

I want to search files for \xc2 but exclude \xc2\xbb

I have grep -rnwl '/home/rascalofthenorth/development/html/' -e "xc2"


Solution

You can also do it using -v option of grep like this:

grep -rnwl '/home/rascalofthenorth/development/html/' -e "xc2" | grep -v "xbb"

The -v option shows only results not containing xbb.

Or for more specific cases in which you want, say, xc2 to appear and xbb not to appear, then awk comes in handy:

awk '/xc2/ && !/xbb/' <yourfile>



Answered By - posixpascal
Answer Checked By - Dawn Plyler (WPSolving Volunteer)