Replace various lines with clipboard content (or from another file).



  • Hello there, I’m in need of a solution for this problem:
    So, I got some text file formatted like this:

    TEXT_00001_ORIG|This is original text.
    TEXT_00001_DEST|

    TEXT_00002_ORIG|An original line.
    TEXT_00002_DEST|

    TEXT_00003_ORIG|More text.
    TEXT_00003_DEST|

    I want to replace all the empty DEST lines with content from the clipboard, or from another txt file, formatted as such:

    TEXT_00001_DEST|This is modified text.
    TEXT_00002_DEST|An altered line.
    TEXT_00003_DEST|Even more text.

    Is there a method to do this without pasting each manually?



  • @pilltwilliams said:

    I want to replace all the empty DEST lines with content from the clipboard, or from another txt file

    There are some old postings that both myself and @guy038 contributed to with respect to a very similar question as you pose. You can select our profiles and look back through the old posts.

    However there can be a number of issues with the solutions. One is that if the size of the file is very large any lookahead included in a regex can fail. But there is another method. I will explain it and hopefully you can attempt it yourself.

    1. In the file you wish to insert the ‘replacement’ text you would first number every line starting from 1. Use the column editor (under edit) and have leading zeros in the number (very important).
    2. Then you would use the ‘Mark’ function to find the lines where you wish to insert the text (under Mark, also tick bookmark). These lines would be cut from the file and inserted into another tab in Notepad++ (NPP).
    3. With the text you wish to insert you would need each insertion on a separate line. You would also number these lines with line numbers starting 1 with leading zeros.
    4. Merge this with the lines cut from the first file and sort numerically. A regex would then combine original line with the ‘replacement’ text.
    5. Copy the new lines back to the original file and sort numerically.
    6. remove line numbers.

    Please note that i haven’t given you actual regular expressions as it will depend on the type of data you working with. Please read the FAQ posts in regards supplying good examples if you wish for more help.

    Terry



  • @Terry-R said:

    These lines would be cut from the file and inserted into another tab in Notepad++ (NPP).

    While proofing my solution i realized i missed a step. With the step 2, once the lines are cut, number the cut lines again (might need a space between the 2 line number sequences), starting at 1 with leading zeros.

    So when this is merged with the text to be inserted the 2 lines will sort and come together. The regex to merge can remove the new line numbers leaving the original line number to be used for a later step.

    Terry



  • Hello, @pilltwilliams, @terry-r and All,

    As your text, William, seems to have literal headers TEXT_#####_ORIG and TEXT_#####_DEST, which clearly identifies the lines, it’s fairly easy to create the appropriate regex S/R ;-))

    So, for instance, assuming that File_A contains the text, even with indented lines, and, possibly, other non-related lines, below :

        TEXT_00001_ORIG|This is original text.
        TEXT_00001_DEST|
    An other
    		TEXT_00002_ORIG|An original line.
    		TEXT_00002_DEST|
    little
    story
        TEXT_00003_ORIG|More text.
        TEXT_00003_DEST|
    
    			TEXT_00004_ORIG|This is
    			TEXT_00004_DEST|
    just
    for
    the
                        TEXT_00005_ORIG|example
                        TEXT_00005_DEST|
    		demonstration
        TEXT_00006_ORIG|the general
        TEXT_00006_DEST|
    

    and that File_B contains the DEST lines, indented or not, with possible non-related lines, as below :

    		  TEXT_00001_DEST|This is modified text.
    		  TEXT_00002_DEST|An altered line.
    12345
    	  TEXT_00003_DEST|Even more text.
    			Some bla
    	  TEXT_00004_DEST|a small
    			blah
    			text for
    				TEXT_00005_DEST|to get
    		testing
    				TEXT_00006_DEST|idea !
    

    Then :

    • Open a new N++ tab

    • Paste the File_A contents in this new tab

    • Insert a line of, at least, 3 dash characters

    • Paste the File_B contents, right after

    Now, the new tab contains :

        TEXT_00001_ORIG|This is original text.
        TEXT_00001_DEST|
    An other
    		TEXT_00002_ORIG|An original line.
    		TEXT_00002_DEST|
    little
    story
        TEXT_00003_ORIG|More text.
        TEXT_00003_DEST|
    
    			TEXT_00004_ORIG|This is
    			TEXT_00004_DEST|
    just
    for
    the
                        TEXT_00005_ORIG|example
                        TEXT_00005_DEST|
    		demonstration
        TEXT_00006_ORIG|the general
        TEXT_00006_DEST|
    
    --------
    
    		  TEXT_00001_DEST|This is modified text.
    		  TEXT_00002_DEST|An altered line.
    12345
    	  TEXT_00003_DEST|Even more text.
    			Some bla
    	  TEXT_00004_DEST|a small
    			blah
    			text for
    				TEXT_00005_DEST|to get
    		testing
    				TEXT_00006_DEST|idea !
    

    • Open the Replace dialog

    • SEARCH (?s)^\h*(TEXT_\d+_DEST\|)(?=\R.+\R\h*\1(?-s)(.+)\R)|^(\h*\R)*----*.+

    • REPLACE ?1$0\2

    • Preferably, check the Wrap around option

    • Select the Regular expression search mode

    • Click once, on the Replace All button or several times on the Replace button

    Et voilà ;-))

    You should get the expected text, below :

        TEXT_00001_ORIG|This is original text.
        TEXT_00001_DEST|This is modified text.
    An other
    		TEXT_00002_ORIG|An original line.
    		TEXT_00002_DEST|An altered line.
    little
    story
        TEXT_00003_ORIG|More text.
        TEXT_00003_DEST|Even more text.
    
    			TEXT_00004_ORIG|This is
    			TEXT_00004_DEST|a small
    just
    for
    the
                        TEXT_00005_ORIG|example
                        TEXT_00005_DEST|to get
    		demonstration
        TEXT_00006_ORIG|the general
        TEXT_00006_DEST|idea !
    
    

    Just note that the line of dashes, -------- and all the subsequent characters have been wiped out, as well ! The number of replacements is the number of lines TEXT_#####_DEST of File_B + 1

    Best Regards

    guy038

    P.S. :

    Be aware that, in some cases ( great amount of data, lot of non-related text between two significant lines, with headers, ... ) this regex S/R could not work properly, and, for instance, wrongly select all file contents :-((


Log in to reply