-1

I have a .csv list of emails + names. Each name can have 1-3 emails along with it (which are currently separated by a comma). I need to convert this into a .csv list where its 1 name and 1 email.

Here is example:

John Smith,johnsmith1@gmail.com,johnsmith2@gmail.com,johnsmith3@gmail.com
Taylor Smith,taylorsmith@gmail.com
Jack Smith,jacksmith1@gmail.com,jacksmith@gmail.com
...(and there are like 10k more rows)

How can I automatically convert this to:

John Smith,johnsmith1@gmail.com
John Smith,johnsmith2@gmail.com
John Smith,johnsmith3@gmail.com
Taylor Smith,taylorsmith@gmail.com
Jack Smith,jacksmith1@gmail.com
Jack Smith,jacksmith@gmail.com

The main problem here attaching the name to separate rows of the emails that were intially with that name.

I appreciate any help - seems like an easy task, but was stuck on this for few days already, Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

-1

If you can use Notepad++ here is a two steps solution:

Replace > Options: Check • Regular expressions, Uncheck [ ] . matches newline

  1. Rearrange line -> Move Name to end of the line
    Search for ^([^,@\n]+),(.+)\R? and replace with $2,$1\n
    Step 1 > demo at regex101 (explanation on the right side)

  2. Now the Name can be captured inside a lookahead and used as a replacement.
    Search for ([^\n,]++)(?=.*,([^\n,@]+$)),(?:(?1)(?:\n|$))? and replace with $2,$1\n
    Step 2 > demo at regex101

    Each comma seperated substring gets captured by the first group while the second group captures the Name inside a lookahead and is put before the E-Mail and removed from the end.

After both steps the result should look like your desired outcome. Further info: SO Regex FAQ

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • Broooooooo thats so dope! It works ++ I almost j***zed my pants from the satisfaction of how smooth this code is, Thx for this reply bro +++ – markothehacker Jul 18 '22 at 13:57
  • @markothehacker Hehe glad that works for you! (you don't need to write answer "another answer works" you can just click on the checkmark to [accept answer](https://stackoverflow.com/help/someone-answers) if it solved it) – bobble bubble Jul 18 '22 at 14:20