How do I merge two or more consecutive lines into one?

  • use a regex, compare 2 sentences, and add all new words on the last of the first sentence.

  • Hello glossar,

    Once again, regexes can do miracles :-)) Just follow these few steps , below :

    • Move back to the very beginning of your file ( Ctrl + Origin )

    • Open the Replace dialog ( Ctrl + H )

    • In the Find what zone, type the regex (?-s)^((.+\t).+)\R\2(.+)

    • In the Replace with zone, type the regex \1, \3 , with a space character, after the comma symbol

    • Uncheck, preferably, the Wrap around option

    • Select, of course, the Regular expression search mode

    • Click on the Replace All button, SEVERAL times, till the message Replace All: 0 occurrences were replaced is displayed, in blue, at the bottom of the Replace dialog

    REMARK :

    • This means that if, in your file, the maximum of consecutive lines with identical contents till the tabulation character, are N, you’ll have to hit N times, on the Replace All button, to get all the work done !

    • Don’t forget that the caret does NOT move during the Replace All operation. So, it should stay stuck to the very beginning of your file !

    NOTES :

    • As usual, the in-line modifier (?-s) ensures you that the dot meta-character will match standard characters, only, even if you previously checked the . matches newline option !

    • Then, the regex engine looks, from beginning of line (^ ) for :

      • Any non-null range of characters ( .+ ), followed by :

      • A tabulation character ( \t ), followed by :

      • Any other non-null range of characters ( .+ ), followed by :

      • Any kind of EOL characters ( \R ), followed by :

      • The group 2, which is ( .+\t ), that is to say, the SAME range of text, between beginning of line and the tabulation included, than in the previous line, and, finally, followed by :

      • Any other non-null range of characters ( .+ ), without line-break, stored as the group 3

    • Now, in replacement, we rewrite :

      • The group 1, whose contents are (.+\t).+. That is to say, all the contents of the first line, without its line-break, followed by :

      • A comma and a space characters

      • The group 3, which represents the part of text, after the tabulation character, in the second line

    It’s not very difficult to understand that, as we, always, rewrite, first, all the contents of a line, followed by the second part of the next line, this process is cumulative on any amount of lines, whose part, before the tabulation character, are identical !

    Hope that this S/R may helps you, Glossar !

    Best Regards,


  • @guy038 said:


    hello Guy38, something is wrong on this formula. Get an error: “Cannot find the text (?-s)^((.+\t).+)\R\2(.+)”

  • Hi, glossar and Vasile,

    Hum… Very strange ! As for me, it works quite fine ! So, Vasile, let’s recapitulate :

    • glossar, from your original text, below :

      Line 344: bördelversuch TAB flanging test
      Line 345: bördelversuch TAB folding test

      Line 28872: führungszapfen TAB guide pilot
      Line 28873: führungszapfen TAB guide pin
      Line 28874: führungszapfen TAB pilot pin

      Line 659368: horizontal geteilt TAB horizontally divided
      Line 659369: horizontal geteilt TAB horizontally split

    • And, taking in account that :

      • The string TAB, with a space character before and after, refers to a single tabulation character \t , of code \x09

      • The part line number #####: , that begins each line, for information, must be deleted

    We, finally, get the text, to work on :

    bördelversuch	flanging test
    bördelversuch	folding test
    führungszapfen	guide pilot
    führungszapfen	guide pin
    führungszapfen	pilot pin
    horizontal geteilt	horizontally divided
    horizontal geteilt	horizontally split


    • Move back the caret before the first line

    • Open the Replace dialog ( Ctrl + H )

    • In the Find what zone, type the regex (?-s)^((.+\t).+)\R\2(.+)

    • In the Replace with zone, type the regex \1, \3 , with a SPACE character, after the comma symbol

    • Select the Regular expression search mode

    • Uncheck all the other options

    • Click THREE times, on the Replace All button, till the message Replace All: 0 occurrences were replaced is displayed, in blue, at the bottom of the Replace dialog

    You should get,as expected, the replaced text, below :

    bördelversuch	flanging test, folding test
    führungszapfen	guide pilot, guide pin, pilot pin
    horizontal geteilt	horizontally divided, horizontally split



  • hello Guy, nope, is not working. Please take a look on this print screen:

    or here

  • Guy - You’re the man! :) Thanks a million! It works! I can’t believe it, but it works!

    Thank you!

    By the way, I like the way you write, your writing style, and enjoy reading your posts! :)

  • No, Vasile, it works!

    Here is my screenshot! :)

    By the way, this “Snaggy” website is cool! Posting a screenshot couldn’t indeed be easier! I have bookmarked it! Thank you! :)

    And finally, I find it a silly practice that new users have to wait for 1200 seconds in order for them to submit their second post!

  • Hello, Vasile,

    Ah, yes ! Quite weird ! From your print screen pictures, everything seems OK : We, both, have the same fields and options and, from the status bar, our encoding and line breaks are also identical !!??

    Moreover, I verified that the Glossar’s text, inserted in your new tab, does have the same displaying than my text ! This implies that you correctly inserted the tabulation characters ( of 4 spaces characters, by default, like me ) !!??

    So, the only thing that could explain the search failure should be, that you, probably, inserted some invisible character(s) in the search regex ?

    But, I must admit that I’m really annoyed to not being able to point out the true reason of your N++'s search behaviour !

    See you later…



    P.S. :

    Glossar, just see your reply to Vasile. Quite pleased that it works as expected, on your configuration !

    BTW, some of us may could test my regex ? May be, it will help us to identify the problem :-)

    One more point, Vasile, which N++ version are you using ?

  • hello. I am using v7.1, no update available.

    But, I had an error yesterday morning, something when N++ had to update Plugin Manager and a Npp plugin, don’t remember very well. I will restart the computer tomorow, and I will test again.

  • no, after restart nothing change. And I installed the v.722, ant still doesn’t work. What a bug is this?

  • @guy038 said:

    Click THREE times, on the Replace All button, till the message Replace All: 0 occurrences were replaced is displayed, in blue, at the bottom of the Replace dialog

    Out of curiosity I tried it, and found that I had to press Replace All only TWO times to get a complete replacement on these three groupings of text. The first press of Replace All does the first and third sets of data; the second press does the second set. However, in the end, it changed the data as expected.

  • Hi, Scott, glossar and vasile,

    Yes, Scott, two times are enough, indeed !. However, I replied to glossar for the general case where you do not know, exactly, what is the maximum of lines with “identical beginnings”, because of a huge file, for instance !

    In that case, it’s better to click on the Replace All button till the message 0 occurrences were replaced appears ! Indeed, as long as yon obtain the message Nocccurrences were replaced, with N > 0, you cannot guess that no more occurrence has to be replaced, the next time :-))



  • @Scott-Sumner

    I press 50 times “Replace all”, and nothing happen. There is a bug somewhere…

  • @Frank-Orellana

    this is working fine !

  • hello guy38, and all friends. Maybe you can improve my regex to resolve the solution (other way).

    So, the regex below will bind (merge) all the sentences into one. The problem is that is not cut the words that repeats.


    Replace with:
    leave space

  • there will be another good solution, that works:

    Find What: ^(\w+\s+\w+\s*)(.*)\n\1
    Replace With: \1\2,

  • @guy038 How can I combine a specific line with a character, ei. Line 2 - 7 should be in line 2 only. and same with the others


  • @guy038

    Hello guy,
    I’ve had to re-visit this topic as I encounter a problem with the regex you had provided above, for which I’ve been grateful to you! It’s weird that the said problem accurs when I join two files (with same line structures, i.e. text-tabulation-text order, and with similar contents/texts) that I’m dealing, whereas the regex has so far worked like a charm. What is odd, is that when I apply the regex on the these files individually/separately it still works, only after I join (two of) them it fails. By joining, I mean adding the whole content of a file to the another, sorting it in Excel, pasting back to txt file. The said problem is that it consumes/deletes all texts/contexts/lines except one line (leaving only a comma behind) usually on the second “Replace all” click, so no “0 occurrances were replaced” message is possible.

    What might causes it?

    I use NP++ v. 7.9.3 (64-bit)

    Many thanks in advance!

  • Hi, @glossar,

    As usual, could you provide the text ( or part of it ) that you get back from Excel, and saved as text in N++ and for which the regex S/R, below, wrongly removes almost everything ?

    SEARCH (?-s)^((.+\t).+)\R\2(.+)

    REPLACE \1,\x20\3



  • @guy038 said in How do I merge two or more consecutive lines into one?:


    Hello guy,

    This is what it lefts behind:

    triplex plunger pump	{TECH&ANGEWANDTE} <convey> Dreiplungerpumpe f.; 3-Plunger-Pumpe f.; Triplex-Plungerpumpe f.,

    There is a tabulation right after the word “pump”, sorry, I can’t make it visible and there is no CRLF at the en of the line.

    It may be useful or necessary for trouble-shooting to put this line in “context” (as we often use it), so here it is with few lines before and after:

    triplex bundle conductor	{ELEKTROTECH} Bündelleiter m. aus drei Teilleitern, Dreierbündel n.
    triplex chain	{TECH&ANGEWANDTE} <driv> Triplexkette f.
    triplex milling machine	{TECH&ANGEWANDTE} <mach/tool> Dreispindelfräsmaschine f.
    triplex operation	{ELEKTROTECH} NRT Triplexbetrieb m.
    triplex plunger pump	{TECH&ANGEWANDTE} <convey> Dreiplungerpumpe f.; 3-Plunger-Pumpe f.; Triplex-Plungerpumpe f.
    triplex pump	{TECH&ANGEWANDTE} <convey> Dreizylinderpumpe f.; Dreikolbenpumpe f.; 3-Zylinder-Pumpe f advt; Triplexpumpe f.
    triplex ram pump	{TECH&ANGEWANDTE} <convey> Dreiplungerpumpe f.; 3-Plunger-Pumpe f.; Triplex-Plungerpumpe f.
    triple-X syndrome	{MEDIZIN} XXX-Syndrom n., X-Trisomie f. (Chromosomenanomalie)
    triplex system	{TELEKOMM} Triplexsystem n.
    triplex winding	{TECH&ANGEWANDTE} <el> Dreifachwicklung f.; Dreilagenwicklung f.; Dreischleifenwicklung f.
    triplex-coated particle	{TECH} n. NUC TECH dreifach beschichtetes Teilchen nt.

    Again, there is a tabulation just before the char “{” on each line, with CRLF at the end.

    And a bit food for trouble-shooting, I’m trying make a “big mama” dictionary file from a series of (smaller) ones, with of course no duplicate headwords, hence applying the regex above. So far I have joined (in the above sense) several of them but there are more to join. Each (new) one of the file I simply add to the already added ones, in a way, I simply glue one after another and on top of another to get the “big mama” after I successfully join all of the files, of course again with no duplicate headwords (=everything before the tabulation is here headword). Suppose, I have three files from which I’d get a “big mama”. I take the first one, sort it in Excel, pasdt back it to txt file, appy the regex, repeat the same process for the second one. Now I simply copy the all content of the second file and paste it to the first one, then I sort it this added content/text in Excel, apply the regex. Now I have a text with no duplicate headwords, but consists the file #1 and #2. The last thing I’d do is to copy the content of the third file, sort it in Excel, past it back to text file, apply the regex to ensure it contains no duplicate headwords before joining it with the already joined file (File1 + File2), coply and past it to this “joined” file, repeat the same process, finally I have the big mama!

    As I said above, the regex works on each separate file, it seems it works as long as the text stays on the same file, but not after transferring one text from one file to another, which at first made me think that it might be with the different encoding issue. But I have checked the encodings of previously successfully joined/added files and verified that encoding doesn’t cause the problem (at least not when one file is UTF-8 and the other is UTF-8-BOM encoded.)

    Now the only thing I could suspect is some char (a non-alphanumeric char or a char that falls beyond coverage of the regex). Since there are thousands of lines in each file, I can’t manually or visually go through to spot them if there is any.

    Sorry for keeping it long this much. Just wanted to give you as much data as possible so that you could trouble-shoot.


Log in to reply