Skip to main content
1 of 4
ata
  • 801
  • 5
  • 8

I want to replace all occurrences of a character's last name with their first name, but only if their first name doesn't come immediately before their last name.

Why don't you find "last name + first name" occurrences and just delete "last name" ? Seems you are over-complicating it.

sed -r 's/(John) Smith/\1/g;s/(Jane) Johnson/\1/g'
ata
  • 801
  • 5
  • 8