Replace carriage returns with commas in Notepad++

There are times when you need comma-separated lists but the data you have is separated by carriage returns instead. In this blog post, I’ll show you how to easily replace carriage returns with commas in Notepad++.

Replace carriage returns or newlines with a comma in Notepad++

replace carriage return with comma in Notepad++
Using extended find and replace mode makes it easy to replace carriage returns with commas in Notepad++ to build a comma-separated list.

There are many examples of this, but in my line of work, I will frequently have a list of IP addresses or hostnames in a spreadsheet. That means if I copy the data out of a spreadsheet, it will be separated by carriage return or newline characters. And I frequently need to import that list into a scanner that needs a comma-separated list.

Notepad++ makes quick work of it. First, paste the list into a new tab in Notepad++.

In Notepad++, the carriage return or the new line is a special character. So you need to use a special mode in its find and replace functionality, called extended mode, to be able to find and replace carriage returns or new lines. When you navigate to Search > Replace or use the CTRL-H keyboard shortcut to bring up the find and replace dialog box, extended mode is one of the options in the bottom left of the dialog box. Just click on the little button next to extended mode.

Then, in the field labeled find, type \n and in the field labeled replace, type the comma character. Then click the button labeled replace all. And I would say 90% of the time, that’s all there is to it.

If you need to go the other direction, with a list separated by commas and you need carriage returns, just do the opposite. Paste the text in Notepad++, and search on the comma character and replace with \n.

What to do when it doesn’t work

There may be times when the output isn’t what you expect, perhaps with some doubled up commas, or inconsistent spacing. You can clean this up after the fact or hit CTRL-Z and go back and look at the original source formatting. In the view menu, there’s an option under Show Symbol to show the end of line characters. You may find there are spaces at the end of the lines that you were not aware of, and using this mode will make that visible.

You can remove spaces at the end of the line by searching for space \n and replacing it with \n. If you have double instances of carriage returns that you don’t want, you can search for \n\n, and replace with a single \n.

But I generally find it easiest to find and replace on double commas with a single comma after the fact just to make sure I have clean output. If I find any in the first round, I just hit replace all until it says it didn’t find any. That takes care of any instances of triples and quads in addition to doubles.

Notepad++ is a very useful editor, and being open source, it’s free to use. When I was working for the vulnerability scanner vendor Qualys, their IT department pre-installed Notepad++ on the laptop they sent me. They didn’t just condone Notepad++, they encouraged it.

If you found this post informative or helpful, please share it!

2 thoughts on “Replace carriage returns with commas in Notepad++

  • April 4, 2023 at 1:29 pm
    Permalink

    Whilst I like notepad++ especially the language specific formatting and printing I would use Word to do what you say here. Just as easy I think?

    • April 4, 2023 at 10:00 pm
      Permalink

      That’s true, you could do the same job in Word and the process would be very similar.

Comments are closed.