Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Negative matching using grep (match lines that do not contain foo)

I have been trying to work out the syntax for this command:
grep ! error_log | find /home/foo/public_html/ -mmin -60

OR:
grep '[^error_log]' | find /home/baumerf/public_html/ -mmin -60


I need to see all files that have been modified except for those named error_log.

I've read about it here, but only found one not-regex pattern.
by

2 Answers

ninja01
grep -v is your friend:

grep --help | grep invert

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

Also check out the related -L (the complement of -l).

-L, --files-without-match only print FILE names containing no match
sandhya6gczb
Use awk for these purposes, as it allows you to perform more complex checks.

Lines not containing foo:

awk '!/foo/'

Lines containing neither foo nor bar:

awk '!/foo/ && !/bar/'

Lines containing neither foo nor bar which contain either foo2 or bar2:

awk '!/foo/ && !/bar/ && (/foo2/ || /bar2/)'

Login / Signup to Answer the Question.