Skip to main content
added 21 characters in body
Source Link
ata
  • 801
  • 5
  • 8
 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Edit

@manatwork,gilles

You're right. How about

sed -r 's/(John Smith)/temp1/g;s/Smith/John/g;s/temp1/John Smith/g'

This seems to do the trick.

 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Edit

You're right. How about

sed -r 's/(John Smith)/temp1/g;s/Smith/John/g;s/temp1/John Smith/g'

This seems to do the trick.

 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Edit

@manatwork,gilles

You're right. How about

sed -r 's/(John Smith)/temp1/g;s/Smith/John/g;s/temp1/John Smith/g'

This seems to do the trick.

added 145 characters in body
Source Link
ata
  • 801
  • 5
  • 8
 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Edit

You're right. How about

sed -r 's/(John Smith)/temp1/g;s/Smith/John/g;s/temp1/John Smith/g'

This seems to do the trick.

 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Edit

You're right. How about

sed -r 's/(John Smith)/temp1/g;s/Smith/John/g;s/temp1/John Smith/g'

This seems to do the trick.

Post Undeleted by ata
deleted 174 characters in body
Source Link
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)[^John] )Smith/\1\1John/g;s/(Jane)[^Jane] )Johnson/\1\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

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'
 sed -r 's/([^John] )Smith/\1John/g;s/([^Jane] )Johnson/\1Jane/g'

The () will capture the non-Firstname before a LastName, so they are backref'd in the replacement.

Post Deleted by ata
Post Undeleted by ata
Post Deleted by ata
Source Link
ata
  • 801
  • 5
  • 8
Loading