Take the 2-minute tour ×  Is it possible, using grep, to search
for instances of John Smith but exclude instances of Mr John
Smith?  share|improve this question add comment  3 Answers  up
vote 2 down vote accepted  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 share|improve this answer
It is experimentally supported in grep: echo $string | grep -P
'(?<!Mr )John Smith'. –  arrange Dec 12 '11 at 19:00 @arrange
Indeed. Thanks, I'll update the answer. –  jcollado Dec 12 '11
at 20:10 Perfect -- just what I was after. Thanks! –  Sam
Hastings Dec 13 '11 at 9:09 add comment  You can execute
command | grep 'John Smith' | grep -v 'Mr John Smith'
share|improve this answer add comment  To use regular
expressions, use ^ and $  grep "^John Smith$"