Reputation: 10111
I want to match a line with foo not followed by bar, e.g.
foo 123 <-- match
foo bar <-- not match
Using the following regex does not work:
echo "foo 123" | grep -E 'foo.*(?!bar).*'
Any idea?
Upvotes: 1
Views: 1168
Reputation: 785266
On systems that don't have grep -P
like OSX you can use this awk
command:
awk -F 'foo' 'NF>1{s=$0; $1=""; if (!index($0,"bar")) print s}' file
Upvotes: 4
Reputation: 174706
You could try the below grep command which uses -P
(perl-regexp) parameter,
grep -P 'foo(?:(?!bar).)*$' file
Example:
$ cat file
foo 123
foo bar
$ grep -P 'foo(?:(?!bar).)*$' file
foo 123
Or
use only negative lookahead to check whether the string bar
is after to foo
without matching any following character.
$ grep -P 'foo(?!.*bar)' file
foo 123
Upvotes: 3