grep for string not preceded by another string

Is it possible, using grep, to search for instances of John Smith but exclude instances of Mr John Smith?


This could be solved using a regular expression with negative lookbehind (which is experimentally supported in grep as pointed out by the comment from arrange):

$ grep -P '(?<!Mr )John Smith' file

Since the support is just experimental, you might want to use perl instead:

$ perl -nle 'print if /(?<!Mr )John Smith/' file

You can execute

command | grep 'John Smith' | grep -v 'Mr John Smith'