I have a textfile like so:
marc_webber
john_grisham
rahmin_darfur
firstname_lastname
I want the output to be like this (forget about double names like "van These"):
Webber, Marc, marc_webber
Grisham, John, john_grisham
Darfur, Rahmin, rahmin_darfur
LastName, FirstName, firstname_lastname
So I want to split the strings at the _, move the last name to the beginning and comma delimit first name and the concatenated name to the end (and maybe even capitalize the first letter).
This would be fairly easy with a programming language, but I want to know whether it is possible using Notepad++'s find and replace feature with regular expressions.
Basically I would need to create variables for the first name and the last name and string them together again in the end.
Except for the casing this should work (Tested in Programmers Notepad though...):
Find Pattern:
((\w+)_(\w+))
Replace Pattern:
\3, \2, \1
This worked for me using sed
(I guess it also works in notepad++ since it probably supports perl regular expressions):
$ sed -r 's/(\w+)_(\w+)/\u\2, \u\1, \0/' file.txt
Webber, Marc, marc_webber
Grisham, John, john_grisham
Darfur, Rahmin, rahmin_darfur
Lastname, Firstname, firstname_lastname
The trick to get the capitalization as in your example is to use \u
.
You can find more information about escape sequences here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With