How do I match all lines not matching a particular pattern using grep
? I tried this:
grep '[^foo]'
How do I match all lines not matching a particular pattern using grep
? I tried this:
grep '[^foo]'
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
-e
option can be used: grep -v -e 'negphrase1' -e 'negphrase2'
Jun 18, 2014 at 9:30
grep -v 'negphrase1|negphrase2|negphrase3'
Aug 5, 2015 at 6:56
-E
works, i.e. grep -vE 'negphrase1|negphrase2|negphrase3'
grep "" /dev/null * | grep foo | grep -v bar | cut -d: -f1 | sort -u
(why the first grep?, there's always a way :))
You can also use awk
for these purposes, since it allows you to perform more complex checks in a clearer way:
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/)'
And so on.
awk
capabilities and providing an alternate solution that provides the same result is helpful.
In your case, you presumably don't want to use grep, but add instead a negative clause to the find command, e.g.
find /home/baumerf/public_html/ -mmin -60 -not -name error_log
If you want to include wildcards in the name, you'll have to escape them, e.g. to exclude files with suffix .log:
find /home/baumerf/public_html/ -mmin -60 -not -name \*.log
mmin
to search for files modified within 60 mins
, use -type f
too as mentioned here stackoverflow.com/a/33410471/2361131