Find all files containing a string but NOT containing another

Find all files containing a string but NOT containing another.

Believe it or not, this was quite a challenge. I tried to use precedent and subsequente regular expressions in UltraEdit for this but UltraEdit’s regex is quite dumb. So I came up with good-old shell:

find . -name "*html" | xargs grep -l 'StringA' | xargs grep -L 'StringB'

Beginning from the current directory (“find .”) this finds all html files containing the string ‘StringA’ in the first grep statement but NOT the ones containing the string ‘StringB’ in the second grep statement. If any nerd could tell me how to do this more easily, this would bring light in my day-to-day life. 🙂 Actually this shell piping is pretty fast. I ran this over hundreds of html files in half of a second. Using PERL you’d need 30 lines and half an hour, I think.

To do this in Windows, you’ll need gfind, xargs and GNU grep (see UnxUtils website)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s