Find and select multiple lines to change order



  • I am trying to figure out how to re-arrange a given set of lines. For example: using regex / part of a macro

    Connection Lost to Camera
    Camera Name1
    Camera Name2
    Account: Customer Name

    Then reorganize it as such:

    Account: Customer Name - Camera Name1 - Connection Lost to Camera
    Account: Customer Name - Camera Name2 - Connection Lost to Camera

    The tricky part is there could be anywhere from one line with Camera name to 20 lines with names. I would want to do this in groups or sets, for example the actual page may look like this:

    Connection Lost to Camera
    Camera Name1
    Camera Name2
    Account: Customer Name

    Video Return
    Camera Name1
    Camera Name2
    Account: Customer Name

    Connection Lost to Camera
    Camera Name1
    Camera Name2
    Account: Customer Name

    When there is an empty line, that will start a new set. Note some start lines will be different ie, Video Return. That would be treated the same as a new set and arranged similarly.



  • Hello, @victor-fansler and All,

    Here’s a solution which needs 2 consecutive regexes S/R :-)


    So, let’s start with this INPUT text that you paste in a new tab :

    Connection Lost to Camera
    Camera Name1
    Camera Name2
    Camera Name3
    Account: Customer Name
    
    Video Return
    Camera Name1
    Account: Customer Name
    
    Fatal Error
    Camera Name1
    Camera Name2
    Camera Name3
    Camera Name4
    Camera Name5
    Camera Name6
    Camera Name7
    Account: Customer Name
    
    • Open the Replace dialog ( Ctrl + H )

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

    • REPLACE \2\1

    • Un-tick all the options

    • Tick the Wrap around option

    • Select the Regular expression search mode

    • Click on the Replace All button ( or several times on the Replace All button )

    => The first line of each block/set ( message ) is moved is order to be the last line of the block/set, as shown below :

    Camera Name1
    Camera Name2
    Camera Name3
    Account: Customer Name
    Connection Lost to Camera
    
    Camera Name1
    Account: Customer Name
    Video Return
    
    Camera Name1
    Camera Name2
    Camera Name3
    Camera Name4
    Camera Name5
    Camera Name6
    Camera Name7
    Account: Customer Name
    Fatal Error
    

    Then :

    • Re-open or go back to the Replace dialog ( Ctrl + H )

    • SEARCH (?-s)^Camera .+(?=\R(?:.+\R)*(Account: .+)\R(.+))|^Account(.+\R){2}

    • REPLACE ?1\1 - $0 - \2

    • Click on the Replace All button ( or several times on the Replace All button )

      • => Each line, beginning with Camera is changed as Account: Customer Name - Camera Name - Error or Info Message

      • => And the last 2 lines of each block/set ( Account: and Error message ) are deleted from current block/set

    And you get the expected OUTPUT list :

    Account: Customer Name - Camera Name1 - Connection Lost to Camera
    Account: Customer Name - Camera Name2 - Connection Lost to Camera
    Account: Customer Name - Camera Name3 - Connection Lost to Camera
    
    Account: Customer Name - Camera Name1 - Video Return
    
    Account: Customer Name - Camera Name1 - Fatal Error
    Account: Customer Name - Camera Name2 - Fatal Error
    Account: Customer Name - Camera Name3 - Fatal Error
    Account: Customer Name - Camera Name4 - Fatal Error
    Account: Customer Name - Camera Name5 - Fatal Error
    Account: Customer Name - Camera Name6 - Fatal Error
    Account: Customer Name - Camera Name7 - Fatal Error
    

    Best Regards

    guy038



  • @guy038 Thanks, the first part works great, but the second part does not. I may not have made it clear, but the line’s “Camera Name” can be anything, they do not actually say “Camera Name”. Then once they are all combined, I will do an addition search that I can easily do, which is to remove the text "Account: " from the beginning of each line. Do a sort of the entire page to group them by Customer name/ Camera name. Again, I can handle the last two parts, just adding for the big picture, or if your solution has a way to do the additional steps in less steps overall.



  • @Victor-Fansler said in Find and select multiple lines to change order:

    . I may not have made it clear, but the line’s “Camera Name” can be anything, they do not actually say “Camera Name”.

    I have an idea, but as not on the PC I will present the concept.

    1. Combine the set together on 1 line by using the empty line between sets to identify them using a special character (or 2) when combining to be used in subsequent steps.
    2. The second regex would be run repeatedly until no changes made by copying the line to below the original, but with only the last camera name, also removing that from the original line at the same time. So not an exact copy.
    3. Tidy up the lines by formatting as required, although this may get possible in step 2.

    Terry



  • Hi, @victor-fansler, @terry-r and All,

    Ah…OK ! But I think, Victor, that the best way would be to show us a list of, let’s say, 4 to 10 initial blocks, as well as the expected layout of these blocks, AFTER modifications !


    Anyway, I give it a new try ! So, starting with this text :

    Connection Lost to Camera
    Line 3 of Set 1
    Line 2 of Set 1
    Line 1 of Set 1
    Account: Christopher
    
    Video Return
    Line 1 of Set 2
    Account: Margaret
    
    Fatal Error
    Line 1 of Set 3
    Line 6 of Set 3
    Line 2 of Set 3
    Line 5 of Set 3
    Camera Name 0
    Line 7 of Set 3
    Line 3 of Set 3
    Account: Elton
    

    With the regex S/R below :

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

    • REPLACE \2\1

    We first get, as before, the following OUTPUT :

    Line 3 of Set 1
    Line 2 of Set 1
    Line 1 of Set 1
    Account: Christopher
    Connection Lost to Camera
    
    Line 1 of Set 2
    Account: Margaret
    Video Return
    
    Line 1 of Set 3
    Line 6 of Set 3
    Line 2 of Set 3
    Line 5 of Set 3
    Camera Name 0
    Line 7 of Set 3
    Line 3 of Set 3
    Account: Elton
    Fatal Error
    

    Then, with this second S/R, slightly modified :

    • SEARCH (?-s)^.+(?=\R(?:.+\R)*Account: (.+)\R(.+)\R)|^(.+\R){2}(?=^\R)

    • REPLACE ?1\1 - $0 - \2

    You should get your expected text :

    Christopher - Line 3 of Set 1 - Connection Lost to Camera
    Christopher - Line 2 of Set 1 - Connection Lost to Camera
    Christopher - Line 1 of Set 1 - Connection Lost to Camera
    
    Margaret - Line 1 of Set 2 - Video Return
    
    Elton - Line 1 of Set 3 - Fatal Error
    Elton - Line 6 of Set 3 - Fatal Error
    Elton - Line 2 of Set 3 - Fatal Error
    Elton - Line 5 of Set 3 - Fatal Error
    Elton - Camera Name 0 - Fatal Error
    Elton - Line 7 of Set 3 - Fatal Error
    Elton - Line 3 of Set 3 - Fatal Error
    

    And, after a classical alphabetical sort, we obtain :

    Christopher - Line 1 of Set 1 - Connection Lost to Camera
    Christopher - Line 2 of Set 1 - Connection Lost to Camera
    Christopher - Line 3 of Set 1 - Connection Lost to Camera
    Elton - Camera Name 0 - Fatal Error
    Elton - Line 1 of Set 3 - Fatal Error
    Elton - Line 2 of Set 3 - Fatal Error
    Elton - Line 3 of Set 3 - Fatal Error
    Elton - Line 5 of Set 3 - Fatal Error
    Elton - Line 6 of Set 3 - Fatal Error
    Elton - Line 7 of Set 3 - Fatal Error
    Margaret - Line 1 of Set 2 - Video Return
    

    I hope that this new layout meets your needs :-))

    BR

    guy038



  • @guy038 said in Find and select multiple lines to change order:

    ?1\1 - $0 - \2

    Thanks, that works. Needs a little cleanup afterwards, but I can deal with that. For some reason it did not remove the last two lines (Account and Connection lost), in the document.

    I wanted to send an actual example of the file, but can’t really share the real information.

    Have a Great Weekend!!



  • Hello, @victor-fansler, @terry-r and All,

    Aaarrrgggh ! Always this damn problem of texts that do not end with a last line break !

    So, before running the regexes, just be sure that your current file ends with a final line-break ( View > Show Symbol > Show All Characters )

    I also, realized that my second regex did not take in account such a possibility !


    Indeed, in the last alternative of my second regex, the part ^(.+\R){2}(?=^\R) searches for 2 complete lines, with their line-breaks ( CRLF ) but ONLY IF it is followed with a pure blank line

    So, either :

    • Use my previous second search regex (?-s)^.+(?=\R(?:.+\R)*Account: (.+)\R(.+)\R)|^(.+\R){2}(?=^\R) and your current file must end this way :
    SECOND to last lineCRLF
    LAST NON-empty lineCRLF
    CRLF
    
    • Or use this new search version (?-s)^.+(?=\R(?:.+\R)*Account: (.+)\R(.+)\R)|^(.+\R){2}(?=^\R|\z) and your current file could simply end as below :
    SECOND to last lineCRLF
    LAST NON-empty ineCRLF
    

    BR

    guy038



  • @guy038 said in Find and select multiple lines to change order:

    Always this damn problem of texts that do not end with a last line break !

    I gave up dealing with that problem long ago when I started to use the editorconfig plugin. With it you can specify that files should always end with an line-ending sequence.

    Too bad that isn’t a native feature of Notepad++.


Log in to reply