Replace new lines with a comma delimiter with Notepad++?

390,315

Solution 1

Open the find and replace dialog (press CTRL+H).

Then select Regular expression in the 'Search Mode' section at the bottom.

In the Find what field enter this: [\r\n]+

In the Replace with:

There is a space after the comma.

This will also replace lines like

Apples

Apricots
Pear

Avocados
Bananas

Where there are empty lines.

If your lines have trailing blank spaces you should remove those first. The simplest way to achieve this is

EDIT -> Blank Operations -> Trim Trailing Space

OR

TextFX -> TextFX Edit -> Trim trailing spaces

Be sure to set the Search Mode to "Regular expression".

Solution 2

Here's what worked for me with a similar list of strings in Notepad++ without any macros or anything else:

  1. Click Edit -> Blank Operations -> EOL to space [All the items should now be in a single line separated by a 'space']

  2. Select any 'space' and do a Replace All (by ',')

Solution 3

fapDaddy's answer using a macro pointed me in the right direction.

Here's precisely what worked for me.

  1. Place the cursor after the first data item. enter image description here

  2. Click 'Macro > Start Recording' in the menu. enter image description here

  3. Type this sequence: Comma, Space, Delete, End. enter image description here

  4. Click 'Macro > Stop recording' in the menu. enter image description here

  5. Click 'Macro > Run a Macro Multiple Times...' in the menu. enter image description here

  6. Click 'Run until the end of file' and click 'Run'. enter image description here

  7. Remove any trailing characters. enter image description here

  8. Done!

enter image description here

Solution 4

For Notepad++ 5.9

  1. Press Ctrl+H
  2. Select Search mode Extended(\n, \r, \t, \o, \x...)
  3. Enter Find what: \r\n
  4. Enter Replace with: ,
  5. Replace_All should get the required result.

Solution 5

A regex match with \s+ worked for me:

enter image description here

Share:
390,315

Related videos on Youtube

user2231530
Author by

user2231530

Updated on September 25, 2020

Comments

  • user2231530
    user2231530 over 3 years

    I have a Notepad++ question.

    How can I take the below words in Notepad++ (which is on different lines)

    Apples
    Apricots
    Pear
    Avocados
    Bananas
    

    And turn them into a paragraph with a comma at the end of each one? Like this:

    Apples, Apricots, Pear, Avocados, Bananas
    
  • user2231530
    user2231530 about 11 years
    Thanks for that tip. What if I have 5000++ words? Do I need to run the macro for all of them individually? Is there an easier method?
  • viclim
    viclim about 11 years
    If you'll have to perform similar task in the future, you might want to write a script for it. Easy way out is probably just go to the Macro Tab and select run the macro multiple times. It will be done in seconds.
  • AdrianHHH
    AdrianHHH about 11 years
    If there are trailing spaces on the line you might for _*[\r\n]+. To deal with the blank lines turned into excess commas you could do a regular expression search for ,_[,_]+ and replace with ,_. Alternatively, the TextFx package has a delete blanks lines command that can be used before adding the commas. (Note change underscores to spaces in these code blocks.)
  • Admin
    Admin about 11 years
    yes you are very correct. I was just trying to give a minimalistic answer. Maybe I should edit the answer then.
  • Iain Samuel McLean Elder
    Iain Samuel McLean Elder almost 11 years
    This doesn't work for me: I see "0 occurrences were replaced."
  • Iain Samuel McLean Elder
    Iain Samuel McLean Elder almost 11 years
    +1 Your answer pointed me in the right direction. In my answer I documented exactly what worked for me.
  • Iain Samuel McLean Elder
    Iain Samuel McLean Elder almost 11 years
    This method is slow for thousands of data items.
  • reggaeguitar
    reggaeguitar about 10 years
    Simple and elegant, nice
  • himanshupareek66
    himanshupareek66 over 9 years
    @trishul Really Nice.
  • Scott Lawrence
    Scott Lawrence over 9 years
    @IainElder, Search Mode must be set to "Regular expression" instead of "Normal" or "Extended".
  • Peter T.
    Peter T. over 8 years
    Great answer. Thanks :)
  • dhochee
    dhochee about 8 years
    I had the same result as Iain.
  • Drew
    Drew over 7 years
    Please improve this answer. Links to images won't be useful.
  • Moin
    Moin about 7 years
    Such a great info, will be useful in many similar situations. Thanks!
  • Høgsdal
    Høgsdal almost 7 years
    This just saved me 10 minutes.
  • AdrianHHH
    AdrianHHH almost 7 years
    This answer repeats the accepted answer and this answer refers to an antique version of Notepad++, version 7.4.x is now available. Welcome to Stack Overflow but please make you answers supply new information or give new insights. Repeating existing answers is not useful.
  • AcePL
    AcePL almost 7 years
    Did not work for me, answer by trisulphani below is waaaaay simpler. Not sure why this one is marked as answer.
  • ironman
    ironman over 5 years
    Thanks for sharing the information. It's interesting.
  • user3673
    user3673 almost 5 years
    BTW: it seems to me that a regex match with $^ should work, but it, instead, matches ^$.
  • nurdyguy
    nurdyguy almost 5 years
    For newer versions of npp this is the better way to to this.
  • nurdyguy
    nurdyguy almost 5 years
    Using the "Extended" option right above the "Regular expression" option (in your screen shot) is actually the easiest way to accomplish this.
  • Mageician
    Mageician over 4 years
    This is the easiest answer. I should not have to go into Regular Expression mode just to replace newlines with commas. N++ actually works correctly whereas other editors interpret "\n" as a carriage return AND line feed, which confused me at first.
  • Nitin Garg
    Nitin Garg about 3 years
    Very simple solution