Replace new lines with a comma delimiter with Notepad++?
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:
Click Edit -> Blank Operations -> EOL to space [All the items should now be in a single line separated by a 'space']
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.
Place the cursor after the first data item.
Click 'Macro > Start Recording' in the menu.
Type this sequence: Comma, Space, Delete, End.
Click 'Macro > Stop recording' in the menu.
Click 'Macro > Run a Macro Multiple Times...' in the menu.
Click 'Run until the end of file' and click 'Run'.
Remove any trailing characters.
Done!
Solution 4
For Notepad++ 5.9
- Press Ctrl+H
- Select Search mode Extended(\n, \r, \t, \o, \x...)
- Enter Find what: \r\n
- Enter Replace with: ,
- Replace_All should get the required result.
Solution 5
A regex match with \s+
worked for me:
Related videos on Youtube
user2231530
Updated on September 25, 2020Comments
-
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 about 11 yearsThanks 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 about 11 yearsIf 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 about 11 yearsIf 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 about 11 yearsyes you are very correct. I was just trying to give a minimalistic answer. Maybe I should edit the answer then.
-
Iain Samuel McLean Elder almost 11 yearsThis doesn't work for me: I see "0 occurrences were replaced."
-
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 almost 11 yearsThis method is slow for thousands of data items.
-
reggaeguitar about 10 yearsSimple and elegant, nice
-
himanshupareek66 over 9 years@trishul Really Nice.
-
Scott Lawrence over 9 years@IainElder, Search Mode must be set to "Regular expression" instead of "Normal" or "Extended".
-
Peter T. over 8 yearsGreat answer. Thanks :)
-
dhochee about 8 yearsI had the same result as Iain.
-
Drew over 7 yearsPlease improve this answer. Links to images won't be useful.
-
Moin about 7 yearsSuch a great info, will be useful in many similar situations. Thanks!
-
Høgsdal almost 7 yearsThis just saved me 10 minutes.
-
AdrianHHH almost 7 yearsThis 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 almost 7 yearsDid not work for me, answer by trisulphani below is waaaaay simpler. Not sure why this one is marked as answer.
-
ironman over 5 yearsThanks for sharing the information. It's interesting.
-
user3673 almost 5 yearsBTW: it seems to me that a regex match with
$^
should work, but it, instead, matches^$
. -
nurdyguy almost 5 yearsFor newer versions of npp this is the better way to to this.
-
nurdyguy almost 5 yearsUsing the "Extended" option right above the "Regular expression" option (in your screen shot) is actually the easiest way to accomplish this.
-
Mageician over 4 yearsThis 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 about 3 yearsVery simple solution