Replace every X(th) line in file 1 with every X(th) line from file 2
-
This post is deleted! -
@Swen-Reindl
There is a fairly simple process to use to achieve your request, although it will take some steps to do so.So for each of the 2 files we will be adding an increasing sequential number at the start (sort of like line numbering), then merging and sorting the lines. Then replacing the doubled up lines.
So in your 1st file, set the cursor position at the very start of the first line. Then use the Edit menu, Column Editor and select “text to insert”. Set this as the
a
character followed by a single space, and click OK. If you’ve got it right EVERY line will start with ana
and a space (be indented 2 character positions). Without moving the cursor, select Column editor again, and this time select number to insert, start with 1, increase by 1 and tick the leading zeros option (very important).
So now all the lines in the first file have their line number at the start, followed by the charactera
.In the second file we have other steps to do first as we only want every 4th line. So we need to remove the other lines first. This we achieve with a regex (regular expression). Copy the content of the 2nd file to a 3rd tab (we will also copy the first file here eventually).
Find What:(.+\R){3}(.+\R)
Replace with:\2
Have the cursor in the first position of the file, very important! Click on Replace all button. As this is a regex, use search mode “regular expression” and tick the “wrap around” so the whole file is processed. Next we will insert 'line numbers and a characterb
, similar to the 1st file. So in your 2nd file (in the 3rd tab where we removed some lines), set the cursor position at the very start of the first line. Then use the Edit menu, Column Editor and select “text to insert”. Set this as theb
character followed by a single space, and click OK. If you’ve got it right EVERY line will start with anb
and a space (be indented 2 character positions). Without moving the cursor, select Column editor again, and this time select number to insert, start with 4, increase by 4 and tick the leading zeros option (very important)Now copy the contents of the 1st file and paste in the 3rd tab at the very bottom (with the edited content of the 2nd file). Next select Edit menu, and then select “sort lines lexicographically ascending”. This will mix up all the lines, so that the two numbered 4 (8,12,16 etc) lines are together, with the
a
line first andb
line second.Now we use a regex (regular expression) to find these ‘duplicate’ lines and edit them (removing the
a
line).So find what:
^(\d+)a .+\R\1(b .+\R)
Replace with:\1\2
Finally we need to remove the leading information we loaded so:
Find what:^\d+[ab]
Replace with:empty field
<---- nothing in this fieldAgain click replace all and you should have the result you want.
Terry
-
@Swen-Reindl
I was proof reading my answer and noticed one slight omission. In the last step where we need to remove the leading numbers and a or b and the space, the space has been omitted. So that regex should be
Find what:^\d+[ab]
<—there is a space at the end of this.
The replace field remains empty.
You will note that I’ve put the regexes in red, this is to prevent the interpreter for these postings interfering with the characters. You can just copy and paste, that’s what we generally tell the requester to do, since this eliminates potential errors with typing.Cheers
Terry -
@Terry-R said:
^\d+[ab]
Looks like I didn’t get away scott free, so here goes it again, this time with different coding.
Find What:^\d+[ab]\h
The
\h
is coding for a space. You can use either, using \h makes it more visual.Terry
-
@Terry-R
Thanks for your reply, it is highly appreciated.
I used my file 1 and file 2 (latters and numbers) as an overly simplified examples.my files actually look like this:
file 1:
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
endfacetfile 2:
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
endfacetI highlighted the lines I want to replace in file 1 with the lines highlighted in file 2 . Hope that clears it out and I’m not sure I will achieve this using your method.
thank you -
Now you realise the importance of showing real data. My solution can work with this new data. There are some issues however so I will outline what needs to occur.
I see in both files the relevant lines are a multiple of what appears to be 7 (not 4 as you first outlined). Also there are 2 lines at the start of both files which will cause issues with my solution. I’d pad out the start of both files with blank lines so the relevant lines to change will start on the 7th line.
Then my solution with the 4 changed to a 7 should work. Once all my steps are completed, remove the blank lines at the start of the new file. I think that should suffice.
Terry
-
@Swen-Reindl
Also you will need to change the {3} to {6} in order to remove the unused lines in the second file.Terry
-
@swen-reindl, @terry-r and All,
Hi, @swen-reindl, if your two files (
File 1
andFile 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 theReplace
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
-
-
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. -
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.
- find
-
@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
-
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.
-
@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
-
@Terry-R said:
If you can provide a statement…
Sorry, meant to say UNLESS you can provide a statement…
-
@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 -
-
@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 lineouter loop
, inFile 1
-
Replace that line with the first line, beginning with the
vertex
string, after a lineouter loop
, inFile 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 theReplace
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 ofvertex.........
lines which must be modified. So, the number3
in that present exampleCheers,
guy038
-