Moving text into a file



  • Hello,
    I would like to move a text on multiple lines delimited with 2 pattern, and move it before another specific pattern in that file.
    Is there a solution? I tried with a macro but even if I can find the searched text defined into both pattern, I don’t succeed to move it at the right place.

    Thanks in advance and best regards
    André



  • @Gasser-André said in Moving text into a file:

    I would like to move a text on multiple lines delimited with 2 pattern, and move it before another specific pattern in that file.
    Is there a solution?

    Your question almost sounds like you are referring to delimited data, often called CSV (comma separated values) files, although the delimiter can be any character you specify. The short answer is yes, in theory.

    The best solution would require knowing more about the locations of the “position to move to” and the “position to remove from” in respect to each other. Also the ability to correctly find the right “field” within the data. A regular expression would locate each area of interest and very likely the text in between both areas. it would then write the text to find and the text in between the 2 areas back in a different order. This effectively switches the location of the text you want to move.

    To provide example data you need to insert it in your post, select it and then click on the </> icon above the posting window. Then provide the example data again with the changes made.

    Terry



  • Hi Terry,
    thanks for answering:
    Here an example: Begin pattern and end pattern are always the same.
    The marked text should be moved between % and O0100 without having to cut and paste manually.
    So I thought with a macro or another function to avoid to have to search into big files the text to move.
    Text2move.png

    Best regards
    André



  • @Gasser-André said in Moving text into a file:

    Here an example: Begin pattern and end pattern are always the same.

    Visually that does help, although if I (or someone else) was to provide a solution you need to provide examples of before and after via the method I outlined before, using the </> icon above the posting window to encapsulate the examples in black boxes. This then means we can actually grab the text to work with and can trust the data not to have been mangled by the posting engine.

    Also is this movement of data to be repeated several times in the file?

    Terry



  • Hi Terry,

    Here you are.

    I can select the text with REGEX ^O9998(.+)M99

    (C1=2345.719)
    (C2=340.242)
    (C3=0.094)
    (C4=1797.305)
    >CD \PATH1\ISO_ALL.TXT
    %
    O0100(1319-0149-104-03.PART - Kanal 1);
    #1134=0;
    M9000P1234;
    N3300M9001P1234;
    M98P3300;
    M9011P1234;
    N3301IF[[#1034AND1]NE1]GOTO3399;
    M0;
    N3399;
    GOTO3400;
    N3400M9012P1234;
    M98P3400;
    M9013P1234;
    N3401IF[[#1034AND1]NE1]GOTO3499;
    M0;
    N3499;
    N3402IF[[#1034AND2]EQ2]GOTO3800;
    N3403IF[[#1034AND8]EQ8]GOTO4000;
    GOTO3500;
    N3500M9014P1234;
    M98P3500;
    M9015P1234;
    GOTO3600;
    N3600M9016P1234;
    M98P3600;
    M9017P1234;
    N3601IF[[#1034AND33]EQ33]GOTO4300;
    N3602IF[[#1034AND8193]EQ8193]GOTO4300;
    N3603IF[[#1034AND1]EQ1]GOTO3700;
    N3604IF[[#1034AND32]EQ32]GOTO4400;
    N3605IF[[#1034AND4096]EQ4096]GOTO4400;
    N3606IF[[#1034AND2]EQ2]GOTO3700;
    N3607IF[[#1034AND8]EQ8]GOTO4200;
    N3608IF[[#1034AND16]EQ16]GOTO4100;
    GOTO3600;
    M99
    O200
    M99
    O300
    M99
    O4000
    M99
    ....
    20000-100000 more lines
    ....
    TOOL#4P11T951A4B4E0;
    TOOL#4P12T950A1234B1234X0Z0Y0E0;
    M99;
    O9998;
    (PROGNUMBER= 116 );
    (PARTNAME= 1319-0149-104-03 );
    (PARTID= 1319-0149-104-03 );
    (PARTTIME= 396976 [MS] );
    (PARTLENGTH= 60.000 );
    (PARTDIAMETER= 9.0000 );
    (MATERIAL= 3.7165 GRADE 5 );
    (MET= 10767-SEL-1 );
    (BDNAME= 2430178-EVODECO16-PTO.MACH );
    (DRAWNO= 10767-SEL-1 );
    (CREATEDATE= 26.03.2020 );
    (D= 000);
    (L= 00000);
    (C= 0000);
    (Z= 00000);
    (Y= 99999);
    (X= 99999);
    (NBISOFILE= 55);
    (PARTUNIT= MM);
    (SHACATOOL= HELIXLOCKINGSCREW-104-00 );
    (MACHINEID= 2430178);
    M99;
    %
    

    And this occurs only one time per file.

    Thanks
    André



  • And after moving

    (C1=2345.719)
    (C2=340.242)
    (C3=0.094)
    (C4=1797.305)
    >CD \PATH1\ISO_ALL.TXT
    %
    O9998;
    (PROGNUMBER= 116 );
    (PARTNAME= 1319-0149-104-03 );
    (PARTID= 1319-0149-104-03 );
    (PARTTIME= 396976 [MS] );
    (PARTLENGTH= 60.000 );
    (PARTDIAMETER= 9.0000 );
    (MATERIAL= 3.7165 GRADE 5 );
    (MET= 10767-SEL-1 );
    (BDNAME= 2430178-EVODECO16-PTO.MACH );
    (DRAWNO= 10767-SEL-1 );
    (CREATEDATE= 26.03.2020 );
    (D= 000);
    (L= 00000);
    (C= 0000);
    (Z= 00000);
    (Y= 99999);
    (X= 99999);
    (NBISOFILE= 55);
    (PARTUNIT= MM);
    (SHACATOOL= HELIXLOCKINGSCREW-104-00 );
    (MACHINEID= 2430178);
    M99;
    O0100(1319-0149-104-03.PART - Kanal 1);
    #1134=0;
    M9000P1234;
    N3300M9001P1234;
    M98P3300;
    M9011P1234;
    N3301IF[[#1034AND1]NE1]GOTO3399;
    M0;
    N3399;
    GOTO3400;
    N3400M9012P1234;
    M98P3400;
    M9013P1234;
    N3401IF[[#1034AND1]NE1]GOTO3499;
    M0;
    N3499;
    N3402IF[[#1034AND2]EQ2]GOTO3800;
    N3403IF[[#1034AND8]EQ8]GOTO4000;
    GOTO3500;
    N3500M9014P1234;
    M98P3500;
    M9015P1234;
    GOTO3600;
    N3600M9016P1234;
    M98P3600;
    M9017P1234;
    N3601IF[[#1034AND33]EQ33]GOTO4300;
    N3602IF[[#1034AND8193]EQ8193]GOTO4300;
    N3603IF[[#1034AND1]EQ1]GOTO3700;
    N3604IF[[#1034AND32]EQ32]GOTO4400;
    N3605IF[[#1034AND4096]EQ4096]GOTO4400;
    N3606IF[[#1034AND2]EQ2]GOTO3700;
    N3607IF[[#1034AND8]EQ8]GOTO4200;
    N3608IF[[#1034AND16]EQ16]GOTO4100;
    GOTO3600;
    M99
    O200
    M99
    O300
    M99
    O4000
    M99
    ....
    20000 more lines
    ....
    TOOL#4P11T951A4B4E0;
    TOOL#4P12T950A1234B1234X0Z0Y0E0;
    M99;
    %
    


  • @Gasser-André said in Moving text into a file:

    Hi Terry,
    Here you are.
    I can select the text with REGEX ^O9998(.+)M99

    Thanks for providing the data so it can be copied and tested with and also the after view.

    As you mentioned in the data that there are many thousands of lines in between original location and the new location I thought we might have an issue with remembering all the data in between so I went with an alternate idea.

    So using the Replace function we have
    Find What:(?s)^(%\R)(?=.+^(O9998;.+)%)|^O9998;.+(%\z)
    Replace With:(?1\1\2)(?3\3)
    This is a regular expression so you need the search mode set to “regular expression”. I think you probably already knew that as you supplied a regex. You should press the “Replace All” button to process the change/edit.

    To give some background, the regex is actually 2 sub expressions, each of which becomes TRUE at a different time. The | is the “alternate character” which defines the 2 sub expressions.

    (?s)^(%\R) - (?s) defines the DOT character as also including the line feed and carriage returns. ^ is start of line followed by the % character on a line all by itself, this is where the text will move to (actually behind it).
    (?=.+^(O9998;.+)) - this is a lookahead to find the text we want in this first position, note we also capture that even though the cursor doesn’t move to the later position
    (?1\1\2) - in the replace with field this states that if the first alternate has run then we return the “%\R” followed by the captured string. At this point the data still resides in it’s original position later in the file, so 2 copies exist.

    So for most of the remainder of the file as the cursor is moving through it both of these sub expressions are tested but neither will find the required character strings. Eventually the 2nd sub expression is TRUE.
    ^O9998;.+(%\z) - find the data that we previously moved, and capture the following % sign, which must be at the end of the file (\z).
    (?3\3) - in the replace field this means that if the 3rd capture group has run then we need to return that information. As the original data is not part of that captured data it is removed and only the % character is written back.

    See if that solves your request. Come back with more information if you find it doesn’t meet your needs and what went wrong with it.

    Terry

    PS the way to test my regex is to note the number of lines in the file before running my solution and again after running it. The number should be the same. Any change means that possibly there are other occurances of the %\R line which allows the data to be moved/copied several times.



  • @Hi Terry

    Thanks a lot for your clear and precise explanation. It works fine.

    Best regards and again many thanks for your help
    André


Log in to reply