Community
    • Login

    Replace every X(th) line in file 1 with every X(th) line from file 2

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    18 Posts 4 Posters 3.4k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Terry RT
      Terry R
      last edited by

      @Swen-Reindl
      Also you will need to change the {3} to {6} in order to remove the unused lines in the second file.

      Terry

      1 Reply Last reply Reply Quote 1
      • guy038G
        guy038
        last edited by guy038

        @swen-reindl, @terry-r and All,

        Hi, @swen-reindl, if your two files ( File 1 and File 2 ) have, exactly, the SAME number of lines and an identical structure, here is my method, different of Terry’s one :

        • Copy the File 1 contents in a new N++ tab

        • Add a simple line of, at least, 3 tildes, right after the contents

        • Now, append the File 2 contents, right after the line of tildes

        • Open the Replace dialog ( Ctrl + H )

        • Type in the regex (?-is)^(outer loop\R).+(?=\R(?:.+\R){21}(.+))|(?s)^~~~.+ , in the Find what: zone

        • Type in the regex \1\2 , in the Replace with: zone

        • 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à ;-))

        Notes :

        • Just not that the number 21, in the search regex, represents the number of lines between the line to change and its corresponding line to replace with ! So, of course, adjust that number with the real gap, in your files

        • After replacing all lines with their counterpart, dont worry ! The regex, then, selects all the remaining text from the tildes line, till the very end of current file, which is deleted

        Too tired, this evening to give you other technical hints regarding that regex S/R. If you would like further explanations, just tell me ;-))

        Best Regards,

        guy038

        P.S. :

        So assuming your two files merged, in a new tab, as below :

        facet normal 0 0 -1
        outer loop
        vertex 0 -4.7850790023803711 3.5992410182952881
        vertex -8.2880001068115234 -9.5701580047607422 3.5992410182952881
        vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
        endloop
        endfacet
        facet normal 0 0 -1
        outer loop
        vertex 0 4.7850785255432129 3.5992410182952881
        vertex 8.2880001068115234 -2.384185791015625e-07 3.5992410182952881
        vertex 0 -4.7850790023803711 3.5992410182952881
        endloop
        endfacet
        facet normal 0 0 -1
        outer loop
        vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
        vertex 0 4.7850785255432129 3.5992410182952881
        vertex 0 -4.7850790023803711 3.5992410182952881
        endloop
        endfacet
        ~~~~~~~~~~
        facet normal -0 -0 -1
        outer loop
        vertex 0 4.7850790023803711 0
        vertex 8.2880001068115234 9.5701580047607422 0
        vertex 8.2880001068115234 -9.5701580047607422 0
        endloop
        endfacet
        facet normal -0 -0 -1
        outer loop
        vertex 0 -4.7850785255432129 0
        vertex -8.2880001068115234 2.384185791015625e-07 0
        vertex 0 4.7850790023803711 0
        endloop
        endfacet
        facet normal -0 -0 -1
        outer loop
        vertex 8.2880001068115234 -9.5701580047607422 0
        vertex 0 -4.7850785255432129 0
        vertex 0 4.7850790023803711 0
        endloop
        endfacet
        

        After performing the regex S/R above, you’re left with the text :

        facet normal 0 0 -1
        outer loop
        vertex 0 4.7850790023803711 0
        vertex -8.2880001068115234 -9.5701580047607422 3.5992410182952881
        vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
        endloop
        endfacet
        facet normal 0 0 -1
        outer loop
        vertex 0 -4.7850785255432129 0
        vertex 8.2880001068115234 -2.384185791015625e-07 3.5992410182952881
        vertex 0 -4.7850790023803711 3.5992410182952881
        endloop
        endfacet
        facet normal 0 0 -1
        outer loop
        vertex 8.2880001068115234 -9.5701580047607422 0
        vertex 0 4.7850785255432129 3.5992410182952881
        vertex 0 -4.7850790023803711 3.5992410182952881
        endloop
        endfacet
        
        1 Reply Last reply Reply Quote 3
        • PeterJonesP
          PeterJones
          last edited by

          given what I know of STL files, I am assuming that’s still an abbreviated form: there are normally a lot more facets than the three @Swen-Reindl showed. So {21} will not be sufficient. Instead, it will be however long file1 was originally.

          1 Reply Last reply Reply Quote 2
          • PeterJonesP
            PeterJones
            last edited by

            Also, (non-Notepad++ STL tangent): @Swen-Reindl , are you sure the facet normals will be correct once the first vertex is changed in each facet? You might want to

            • find (?-s)(facet normal).*$
            • replace $1 0 0 0

            to make sure the normals are all zeros – most STL-interpreting software will then automatically calculate the correct normal based on the right-hand rule; some might get confused if the provided normal is wrong-but-nonzero.

            1 Reply Last reply Reply Quote 1
            • guy038G
              guy038
              last edited by guy038

              @swen-reindl, @terry-r, @peterjones and All,

              Ah…yes, I was just seeing “raw” text, unlike you, who correctly detected the files type ;-))

              As Terry said :

              Now you realise the importance of showing real data

              So, just wait for the exact data structure of the @swen-reindl’s STL files !

              Cheers,

              guy038

              1 Reply Last reply Reply Quote 0
              • Swen ReindlS
                Swen Reindl
                last edited by

                I suppose what I need is:
                Replace the next line, or the line after that - which follows after the line “outer loop” in file 1 with corresponding line in file 2.

                I think that’s a better explanation.

                Would that make it easier?

                Sorry for the confusion.

                1 Reply Last reply Reply Quote 0
                • Terry RT
                  Terry R
                  last edited by Terry R

                  @Swen-Reindl said:

                  Replace the next line, or the line after that - which follows after the line “outer loop” in file 1 with corresponding line in file 2

                  When I read that I cringe! How do you determine which line to replace. If you can provide a statement under which condition the 1st (of the 2 lines) or the second (of the 2 lines) is the one being replaced then I don’t see how we can create a regex to do it.

                  Regexes aren’t intelligent. they are essentially a small program and will blindly do what is tasked of them, rightly or wrongly.

                  Terry

                  1 Reply Last reply Reply Quote 1
                  • Terry RT
                    Terry R
                    last edited by

                    @Terry-R said:

                    If you can provide a statement…

                    Sorry, meant to say UNLESS you can provide a statement…

                    1 Reply Last reply Reply Quote 0
                    • Swen ReindlS
                      Swen Reindl
                      last edited by

                      @PeterJones
                      that is correct, those are simple stl files… my goal is to “mix” files with a larger amount of facets. I done what I would like to achieve manually on a picture below (.txt in the image at the end)
                      https://i.imgur.com/fd7FTMN.png

                      @guy038
                      I do not have any specific data/files, that’s why I’m only using this simple example. I would like to combine any 2 .txt files into a one .txt mixed file at the end. So there could be 1000+ lines.

                      @Terry-R
                      Sorry, my bad for stating it wrong. I would like to replace first line following after each “outer loop” (in file 1) with the first line following “outer loop” in file 2. I hope image helps better than words in my case. Thanks for your patience.
                      https://i.imgur.com/PEVv5M6.png

                      1 Reply Last reply Reply Quote 1
                      • PeterJonesP
                        PeterJones
                        last edited by

                        I think, then, that @Terry-R’s existing solution is your best bet, unless you have some example of why it doesn’t work for your needs. @guy038’s is nice, but it has to be specifically tailored to the given filesize.

                        1 Reply Last reply Reply Quote 1
                        • guy038G
                          guy038
                          last edited by guy038

                          @swen-reindl, @terry-r, @perterjones and All,

                          The drawback of my first method is that you needed very similar structure of the two files, in order to keep a constant gap between the line to replace and the line which must replace it :-(

                          So, here is my second attempt, which needs two regex S/R but which can be used in all cases, whatever the file’s structure ! Indeed, it simply :

                          • Searches for the first line, beginning with the vertex string, after a line outer loop, in File 1

                          • Replace that line with the first line, beginning with the vertex string, after a line outer loop, in File 2


                          • Copy the File 1 contents in a new N++ tab

                          • Add a simple line of, at least, 3 tildes, right after the contents

                          • Now, append the File 2 contents, right after the line of tildes

                          So, let’s suppose that we get this sample text, where I added some bla bla bla lines, at random locations

                          # File 1
                          
                          facet normal 0 0 -1
                          bla bla
                          bla bla blah
                          outer loop
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex -8.2880001068115234 -9.5701580047607422 3.5992410182952881
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          facet normal 0 0 -1
                          outer loop
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex 8.2880001068115234 -2.384185791015625e-07 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          endfacet
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          facet normal 0 0 -1
                          outer loop
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          ~~~~~~~~~~
                          # File 2
                          
                          facet normal -0 -0 -1
                          outer loop
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850790023803711 0
                          bla bla
                          bla bla blah
                          vertex 8.2880001068115234 9.5701580047607422 0
                          vertex 8.2880001068115234 -9.5701580047607422 0
                          endloop
                          endfacet
                          facet normal -0 -0 -1
                          outer loop
                          vertex 0 -4.7850785255432129 0
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex -8.2880001068115234 2.384185791015625e-07 0
                          vertex 0 4.7850790023803711 0
                          endloop
                          endfacet
                          facet normal -0 -0 -1
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          outer loop
                          bla bla
                          bla bla blah
                          vertex 8.2880001068115234 -9.5701580047607422 0
                          vertex 0 -4.7850785255432129 0
                          vertex 0 4.7850790023803711 0
                          endloop
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          endfacet
                          
                          • Now , open the Replace dialog ( Ctrl + H )

                          • Type in the regex (?s).+?(?|(\R~~~+)|outer loop.*?(?-s)(\Rvertex .+))|(?s).+ , in the Find what: zone

                          • Type in the regex \1 , in the Replace with: zone

                          • Check the Wrap around option

                          • Select the Regular expression search mode

                          • Click, once, on the Replace All button

                          You should obtain this shorter text, with only the lines which need to be replaced and their counterparts which must replace them ;-))

                          vertex 0 -4.7850790023803711 3.5992410182952881
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          ~~~~~~~~~~
                          vertex 0 4.7850790023803711 0
                          vertex 0 -4.7850785255432129 0
                          vertex 8.2880001068115234 -9.5701580047607422 0
                          

                          Then , it’s fairly obvious :

                          • Insert the File 1 contents at the very beginning of that new tab

                          • Add, again, a simple line of, at least, 3 tildes, right after the contents

                          Hence, the new tab contents becomes :

                          # File 1
                          
                          facet normal 0 0 -1
                          bla bla
                          bla bla blah
                          outer loop
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex -8.2880001068115234 -9.5701580047607422 3.5992410182952881
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          facet normal 0 0 -1
                          outer loop
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex 8.2880001068115234 -2.384185791015625e-07 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          endfacet
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          facet normal 0 0 -1
                          outer loop
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          ~~~~~~~~~~
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          ~~~~~~~~~~
                          vertex 0 4.7850790023803711 0
                          vertex 0 -4.7850785255432129 0
                          vertex 8.2880001068115234 -9.5701580047607422 0
                          
                          • Open, again, the Replace dialog ( Ctrl + H )

                          • Type in the regex ^outer loop(?s).*?\K(?-s)(^vertex.+)(?=(?s:\R.+?\R~~~.+?)\1\R(?:.+\R){3}(.+))|(?s)^~~~.+ , in the Find what: zone

                          • Type in the regex \2 , in the Replace with: zone

                          • Check the Wrap around option

                          • Select the Regular expression search mode

                          • Click, once, on the Replace All button ( Do NOT use the Replace button, because of the \K feature in the search regex )

                          Here we are ! We get, in our new tab, just the new File 1 contents, after all the replacements ;-))

                          # File 1
                          
                          facet normal 0 0 -1
                          bla bla
                          bla bla blah
                          outer loop
                          bla bla
                          bla bla blah
                          vertex 0 4.7850790023803711 0
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex -8.2880001068115234 -9.5701580047607422 3.5992410182952881
                          vertex -8.2880001068115234 9.5701580047607422 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          facet normal 0 0 -1
                          outer loop
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850785255432129 0
                          vertex 8.2880001068115234 -2.384185791015625e-07 3.5992410182952881
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          endfacet
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          facet normal 0 0 -1
                          outer loop
                          vertex 8.2880001068115234 -9.5701580047607422 0
                          bla bla
                          bla bla blah
                          bla bla
                          bla bla blah
                          vertex 0 4.7850785255432129 3.5992410182952881
                          vertex 0 -4.7850790023803711 3.5992410182952881
                          endloop
                          bla bla
                          bla bla blah
                          endfacet
                          

                          Note that the {3} quantifier, in the search regex, is, exactly, the number of vertex......... lines which must be modified. So, the number 3 in that present example

                          Cheers,

                          guy038

                          1 Reply Last reply Reply Quote 2
                          • First post
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors