Combine 2 files line by line



  • @PeterJones said in Combine 2 files line by line:

    That is a good addendum to that older post.

    As Peter said, that is a good addendum to THAT-OLDER-POST. :-)



  • Hello, @ fernando-sorensen, @peterjones and All,

    Indeed, very clever method, Fernando ;-))


    Now, assuming the input text, below, where I, deliberately, added some Tabulation or Space chars and sometimes none :

    1000	   		Line A in File 1
    1001	   		Line B in File 2
    1002Line C in File 1
    1003  Line D in File 2
    1004    Line E in File 1
    1005		Line F in File 2
    1006Line G in File 1
    1007	Line H in File 2
    1008 Line I in File 1
    1009	     Line J in File 2
    

    You may want to place the lines, of same number, in the same resulting line and delete the temporary leading numbers, in one go. If so, and assuming that your list begins with an even number, use the following regex S/R :

    SEARCH (?-s)^\d+[02468]\h*(.+)\R\d+\h*   OR   (?-s)^\d+[13579]\h*(.+)\R\d+\h* if the list begins with an odd number

    REPLACE \1\x20

    And you’ll get your expected output text :

    Line A in File 1 Line B in File 2
    Line C in File 1 Line D in File 2
    Line E in File 1 Line F in File 2
    Line G in File 1 Line H in File 2
    Line I in File 1 Line J in File 2
    

    The space separator character \x20, used in replacement may be changed to \t or \x20\x20\x20 or, even, \x20+\x20 !


    Of course, if you just would like to get rid of the temporary leadings blank chars, beginning each line, simply use :

    SEARCH ^\d+\h*

    REPLACE Leave EMPTY

    And, this time, you’re left with :

    Line A in File 1
    Line B in File 2
    Line C in File 1
    Line D in File 2
    Line E in File 1
    Line F in File 2
    Line G in File 1
    Line H in File 2
    Line I in File 1
    Line J in File 2
    

    Best Regards,

    guy038



  • @Alan-Kilborn said in Combine 2 files line by line:

    As Peter said, that is a good addendum to THAT-OLDER-POST. :-)

    Oopps, didn’t notice until now that the OP linked to that older post at the very beginning; sorry for the extraneous traffic.
    And now even more traffic, to point that out.
    :-(



  • Hi @Fernando-Sorensen and All:

    Yet another way to combine two blocks of text. It’s really easy to implement and requires just the multi-edition preference enabled and the BetterMultiSelection plugin activated, which you can download and install via the Plugins Admin.

    Firstly an animation and below a step by step description:

    Giphy

    1. Place the caret at the beginning of the second text block, which will be the inserting text.
    2. Do a block selection, that is, mouse-click at the left of character 2 and then another mouse-click at the end of the same block, but this time while pressing Shift + Alt.
    3. Copy the selected block (Ctrl + C).
    4. Go to the first character of first block text (Ctrl + Home) and mouse-click at the left of the first 1.
    5. Insert a multi-selection by pressing Shift + Alt + Arrow Down 4 times, till the bottom line of the text block.
    6. Press Arrow Down one more time (without Shift or Alt) in order to move the multi-selection one line down.
    7. Press Enter to add five lines to the text block.
    8. Paste (Ctrl + V) to combine both text blocks.

    That’s all. Hope you like it.



  • Hi, @astrosofista and all,

    A small variant :

    At step 6, press the End key. I think it’s easier to understand ;-))

    BR

    guy038



  • So, really @astrosofista has found some amazing uses for the BetterMultiselection plugin.
    It makes one wonder, why isn’t this in Notepad++ itself (without need for the plugin)?
    @dail 's work with the plugin is really great, but why do we even need a plugin?
    I guess it would need to be in Scintilla, but these actions just make so much sense I wonder if the capabililty IS in Scintilla, and just not enabled in Notepad++'s use of Scintilla?
    Thoughts?



  • @Alan-Kilborn

    The newer versions of scintilla (which N++ is using) has a bit better support. However it still doesnt match what my plugin provides.

    I think the key is that Scintilla does not transform a rectangular selection into multiple stream selections.

    If I recall correctly, this was brought up on the Scintilla list or tracker and they concluded this functionality would not be added to Scintilla. There may be more technical reasons as to why it was not seen as beneficial, however I feel this plugin demonstrates how useful this kind of behavior is.



  • @dail said in Combine 2 files line by line:

    they concluded this functionality would not be added to Scintilla

    Bad news, in a way. In another sense it means that your plug-in will be guaranteed to have a long life.

    Please keep developing it. Thank you.



  • Hi @guy038

    That’s right, the End key will get the same outcome, though I’m not sure it’s any easier to understand. Anyway, good catch :)

    By the way, shouldn’t all the multi-carets be shown in red? At least that’s what I expected to see, since I wanted them to stand out in the animation.



  • @astrosofista said in Combine 2 files line by line:

    shouldn’t all the multi-carets be shown in red? At least that’s what I expected to see,

    Curious, why do you expect this?
    Typically the caret related to the main selection is supposed to visually “stand out” from any secondary carets. Several Notepad++ functions act only on the main selection, so sometimes it is helpful to know which this is going to be before invoking the function.

    In Pythonscript, I do this to make the different carets stand out well:

        for e in (editor1, editor2):
            e.setCaretFore((0xFF, 0, 0))  # turn the main caret bright red for better visibility
            e.setAdditionalCaretFore((0, 0, 0))  # turn the secondary carets black for good visibility
    


  • Hi @Alan-Kilborn

    For a reason of consistency. I expected to see all carets in the same color but not in the same shade, just as if I set the caret color to black in the Style Configurator, then the secondary carets are shown in gray.



  • @astrosofista

    So then how are you actually getting different colored carets and not just the same-base-color-but-different-shade effect?
    Maybe you don’t know and this is your question?

    Anyway, forgot to include the screenshot I made of my custom-colored carets; here it is:

    d16efd7d-e181-4f87-9f5e-4b9bf6dbc8f8-image.png



  • @Alan-Kilborn said in Combine 2 files line by line:

    So then how are you actually getting different colored carets and not just the same-base-color-but-different-shade effect?
    Maybe you don’t know and this is your question?

    That’s my question, yes.

    Concerning the screenshot, will try the code, though I will probably set all the carets to the same base color, just to check the effect.



  • @astrosofista I use a method similar to yours that works without any plugins (unless those are default plugins that I also have). I add blank lines to both the source and destination block (search: [\r\n]+ replace: \r\n\r\n. I then Alt+Shift block select the source block, cut, position the cursor on the first blank line in the destination and paste.





  • Hello, @cooeeeee and All,

    Ah…! Many thanks for that tip and work-around ! I didn’t even know that such an operation was possible ! Note that it’s works whatever the BetterMultiSelection plugin of @Dail is enabled or not ;-))

    Note that you may shorten your regex S/R as below :

    SEARCH \R

    REPLACE $0$0

    Best Regards,

    guy038



  • @guy038 and others:

    What allows it to work, is an odd(?) property of column blocks – the inability to copy line-endings.

    Here’s some text before blocking out a column, note where the CRLF line-endings are:

    809a4cf5-49cd-4d1e-9fd2-5c55363607cc-image.png

    Now here’s the same text after a rectangular block is put into place; again note where the CRLF are (they’re outside the selection), especially on the empty lines:

    4f9678ee-ce37-47ff-acc0-705ccf1a51b8-image.png

    Note that none of the line-ending characters made it into the block.
    Thus when this is copied and pasted, the originally empty lines are pasted as “nothingness”, allowing the technique by @Cooeeeee to work for merging data. Nice one.



  • Hi @Cooeeeee

    I see. The point of my post was to simplify @Fernando-Sorensen’s method, because although ingenious, it requires several steps. The basic idea was to do everything without additional windows, be it a second editor, the column editor or a search window, that would block or cut off the flow of the operation.

    The only thing that the BetterMultiSelection plugin is good for in my approach is precisely to insert the blank lines, an operation that standard multiselection cannot do - see @Alan-Kilborn’s post below - . But if the text already has blank lines interleaved, then the plugin is not necessary and, as your .gif shows, line interleaving can be done directly with standard multiselection.

    Which is the best method? For the reasons detailed above and because I have nothing against third party plugins either - in fact, I think they are a great addition to Notepad++ and heavily use them - I still prefer mine, but I have no objection to people using the one they find best.

    Thank you and, as always, have fun!


Log in to reply