Filling empty lines with previous line



  • Hi All!
    I’d like to request a bit of help with the following.

    I have a document with words and expressions and random number of empty lines between them.

    I’d like to fill all the empty ones with the preceding content. If the number of empty lines is more than one, every single one should repeat the previous “occupied” line.


    So from this one:

    apple
    watermelon
    empty line
    orange
    pear
    empty line
    empty line
    empty line
    empty line
    cherry and mango
    empty line
    strawberry
    empty line
    empty line
    nectarine
    plum, banana and raspberry
    empty line
    empty line
    empty line
    empty line
    empty line
    empty line
    grape
    empty line
    mango
    empty line
    blueberry and mandarin
    kiwi
    empty line
    empty line
    papaya


    I’d like to achieve the following one:

    apple
    watermelon
    watermelon
    orange
    pear
    pear
    pear
    pear
    pear
    cherry and mango
    cherry and mango
    strawberry
    strawberry
    strawberry
    nectarine
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    plum, banana and raspberry
    grape
    grape
    mango
    mango
    blueberry and mandarin
    kiwi
    kiwi
    kiwi
    papaya


    What way can I achieve this?

    Thank you and have a nice day,
    Viktoria



  • Do Replace All repeatedly with this regular-expression replacement:

    find: (?-s)^(.+?)\R\R
    repl: ${1}\r\n${1}\r\n

    Tick Wrap around



  • Hello, @viktoria-ontapado, @alan-kilborn and All,

    This regex S/R should be OK, too :

    • SEARCH (?-s)(.+\R)\R

    • REPLACE \1\1

    • Tick the Wrap around option

    • Select the Regular expression search mode

    • Click, repeatedly, on the Replace All or, best, use the Alt + A shortcut

    Best Regards,

    guy038



  • Hello, @viktoria-ontapado, @alan-kilborn, @guy038

    As an exercise to see if another way could be found I came up with this. I haven’t been able to create a 1 step regex, but these 2 different regexes each run only once.

    So the method is to move the lines around so the line to be copied is below those empty lines for each “set”. Then using a positive lookahead we capture the “line” to be copied and replace the current “empty” line with that text.

    Maybe it might prompt those amongst us (looking at you @guy038) to pull apart my idea to see if it can be made into a 1 step operation.

    First regex which changes the order of the lines in each set.
    Find What:(^.+\R)(\R+)
    Replace With:\2\1

    Second regex which fills in the empty lines with the text to be copied.
    Find What:(^\R)(?=\R*?(.+\R))
    Replace With:\2

    So each regex only needs running once using the “Replace All” button.

    Oh and contrary to above statement to have wrap around ticked IMO it should NOT be ticked. The cursor should be in the very first position of the file before beginning any of these regex, since the filling of empty lines is crucial on position of cursor when commencing.

    Terry



  • Hello,@Viktoria-Ontapado

    Please follow this step, To Filling empty lines with the previous line

    Step 1:- Press Ctrl+H (Replace)
    Step 2:- Select Extended from SearchMode
    Step 3:- Put \r\n\r\n in Find What
    Step 4:- Put \r\n in ReplaceWith
    Step 5:- Click on Replace All

    I hope this information will be useful for you.
    Thank you.



  • Alternatively using the python script plugin and the following python should achieve the desired results:

    from Npp import *
    
    def replaceEmptyLinesWithPrevious(contents, lineNumber, totalLines):
        if len(contents.strip()) == 0:
            previousLine = editor.getLine(lineNumber-1)
            editor.replaceWholeLine(lineNumber, previousLine)
        return 1
        
    
    editor.forEachLine(replaceEmptyLinesWithPrevious)
    


  • As usual, I really appreciate the answers, very impressive solutions. I tested the script as well, works like a charm. Thank you so much for the invaluable help.

    Have a nice day,
    Viktória


Log in to reply