SED – quick inline editor

EDIT .. re your comment.. Here is a new script which doesn’t concern itself about (eg.) William Smith. It temporarily obfuscates patterns which it keeps as Smith (unchanged).

sed -r ‘s/\<(John) (Smith)\>/\1\x01x\2/g;
s/\/John/g; s/\x01x/ /g’

If you are concerned about Mr. Mr Mrs… then this works.

sed -r ‘s/\<(John|((M(r|rs|s))\.?)) (Smith)\>/\1\x01x\5/g
s/\/John/g; s/\x01x/ /g’

You can cater for William by adding his name to the or list, eg.
sed -r ‘s/\<(William|John|... This is the orginal script sed -r 's/(^|[[:punct:]] |\<[a-z]+ )(Smith\>)/\1John/’