Skip to main content
Known Participant
December 30, 2016
Answered

Grep find/replace to switch Lastname, First M to First M Lastname

  • December 30, 2016
  • 4 replies
  • 2726 views

I almost have it but have run into a snag that i can't figure out...Hyphenated names

I have to create a index for a directory

I have pulled all the names using "create table of content" from the document that use a certain paragraph style

in the directory they are listed last, first but here they want first, last so i made a find replace using greps that flips them

FIND              ^(\w+), (\w+)([.\w]+)?

REPLACE     $2$3 $1

works fine for Rudy, Scott M

but fails on     Rudy-Goon, Scott M

when it fails it does nothings it would look like the list on the right below after i run it

Rudy, Scott M                 Scott M Rudy

Rudy-Goon,Scott M          Rudy-Goon,Scott M

Doe, John S                    John S Doe

Public, John Q                John Q Public

any suggestions?

thanks in advance!!!!

scott

This topic has been closed for replies.
Correct answer Trevor:

Try ^([-\w]+), ([-\w+])([-.\w]+)?

Note you have a typo and are missing the space in after the "," in the problematic line, so the above grep won't work either unless you add the space.

HTH

Trevor

4 replies

Participant
May 22, 2020

Does anyone know how to change firstname_lastname to lastname, firstname?

Jongware
Community Expert
Community Expert
May 22, 2020

That would be

Find: (.+?)_(.+)

Change to: $2, $1

 

If this does not work for you, post a new question and include examples.

Participant
August 21, 2023

How do you change lastname, firstname to firstname lastname
None of the above are helping me?

winterm
Legend
December 30, 2016

Just another approach...

@Js1212Author
Known Participant
December 30, 2016

Thank you all for the help...You all make it look so easy to do this and it seems so obvious AFTER i see the answer--like a good riddle

foltman
Known Participant
December 30, 2016

Maybe this one helps you:

find: ^((\w+)(-\w+)?), ?(\w+ \w)

change to: $4 $1

I've added " ?" after comma if this kind of typos that Trevor mentioned are more frequent.

Cheers,jf

Trevor:
Trevor:Correct answer
Legend
December 30, 2016

Try ^([-\w]+), ([-\w+])([-.\w]+)?

Note you have a typo and are missing the space in after the "," in the problematic line, so the above grep won't work either unless you add the space.

HTH

Trevor