Conditional search and Append or Replace - Help needed.
-
Hello everyone.
I’m trying to automate a search and replace with regex expresions to add a conditional text after a line of a GCode. It’s a 3D printer Gcode file with near a million lines.
The GCode Text is plain text, each instruction code in one line. It looks like this (resumed):
T0 S120
T1 S200
M109 T0 S200
M109 T1 S205
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T1
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T0
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T1
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T0
; After Head changeI want to add some code lines after the T0 or T1 lines, but when the preceding line its “:Head Change”. When its T0 insert at next line “T0 script lines” and when its T1 insert at next line “T1 script lines”.
I don’t want to add or remove nothing when T0 or T1 it isn’t preceded by “;Head Change” line or if T0 or T1 are next to other text in the same line. Just add after a T0 or T1 it is alone.The result would look like this:
T0 S120
T1 S200
M109 T0 S200
M109 T1 S205
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T1
G1 line 1 tool 1 script
G1 line 2 tool 1 script
G1 line 3 tool 1 script
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T0
G1 line 1 tool 0 script
G1 line 2 tool 0 script
G1 line 3 tool 0 script
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T1
G1 line 1 tool 1 script
G1 line 2 tool 1 script
G1 line 3 tool 1 script
; After Head change
;LAYER:50
G92 E0
G1 F2400 E-16.00000
; Head Change
T0
G1 line 1 tool 0 script
G1 line 2 tool 0 script
G1 line 3 tool 0 script
; After Head changeI’m trying different regex expresions but i’m unable to get the search and replace (or append) work.
Can you help me a bit?
Sorry, English isn’t my languaje.
-
This is not too difficult, as long as I truly understand your meaning… However, at the very least, your BEFORE text can be turned into your AFTER text.
Here’s how:
Find-what box:
; Head Change\RT([01])\R
Replace-with box:$0G1 line 1 tool \1 script\r\nG1 line 2 tool \1 script\r\nG1 line 3 tool \1 script\r\n
Search mode:Regular expression
Options:Match case
(if you feel you need it)Here’s how it works:
Finding:
- match literal
; Head Change
followed by a line-ending followed by a literalT
- match if next character is 0 or 1, remember what it was as group #1 (needed later)
- match if a line-ending follows the 0 or 1
Replacing:
- replace the complete match with itself (we want to keep the existing text)
- right after that add 3 similar lines (with Windows line-endings) of specified text, substituting in the remembered group #1 data at the correct point in each
If this (or ANY other posting on the Notepad++ Community site) is useful, don’t reply with a “thanks”, just up-vote ( click the
^
in the^ 0 v
area on the right ). - match literal
-
Just to clarify, there are other T0 or T1 in the code, but i just want to replace/add the ones who are preceded by the line “;Head Change” as condition to add/replace.
I’ve tried "(; Head Change**\n**T.)/gm in regexr.com and result all the needed changes after these lines but NP++ seem to not understand \n CR escape code.
Thank you for your time.
-
@Marcos-Souto-Fernandez said:
i just want to replace/add the ones who are preceded by the line “;Head Change”
Yes, my solution above obeys this.
I’ve tried "(; Head Change**\n**T.)
Maybe the
**
in your posting is just you trying to do markdown around the\n
? Hopefully, because if not then that looks like an invalid regular expression.Notepad++ understands
\n
but it has to match your data. If you have Windows line-endings (the most common case), then you want to use\r\n
in regular expressions to match those line-endings. If you have Linux line-endings, then\n
on its own will match them. Even better is to just use the\R
(note the capitalR
), which will match either type (but unfortunately it cannot be used in the Replace-with box, only the Find-what box). -
Thanks Scott.
My problem is that the T0 script text is diferent from T1 script, not simply put a 0 or 1 on the text lines.
When a T0 is found after “;Head Change” i need to append after this lines:
;When T0
G1 F12000
G1 X300 Y50
G1 X300 Y0
G1 X280 Y20
G1 X245 Y0
G1 X245 Y50And when a T1 is found in the same circumstances, i need to a append this ones:
;When T1
G1 F12000
G1 X245 Y50
G1 X245 Y0
G1 X225 Y20
G1 X300 Y0
G1 X300 Y50Maybe i’had fail explaining it. I’m sorry.
-
It often occurs that people leave out crucial details when explaining their problem. No worries, it can still be done!
Find-what box:
; Head Change\RT(?:(0)|(1))\R
Replace-with box:$0(?1this\r\nfollows\r\na T-ZERO!)(?2THIS\r\nFOLLOWS\r\nA T-one!)\r\n
This replacement will turn your original text into the following (note I just inserted some placeholder text in the output; you can adjust as needed for your exact text):
T0 S120 T1 S200 M109 T0 S200 M109 T1 S205 ;LAYER:50 G92 E0 G1 F2400 E-16.00000 ; Head Change T1 THIS FOLLOWS A T-one! ; After Head change ;LAYER:50 G92 E0 G1 F2400 E-16.00000 ; Head Change T0 this follows a T-ZERO! ; After Head change ;LAYER:50 G92 E0 G1 F2400 E-16.00000 ; Head Change T1 THIS FOLLOWS A T-one! ; After Head change ;LAYER:50 G92 E0 G1 F2400 E-16.00000 ; Head Change T0 this follows a T-ZERO! ; After Head change
This regular expression replacement is needfully more complicated than the earlier try. The Find-what part matches a
0
and remembers it as group #1, or it matches a1
and remembers it as group #2. At replace time, it TESTS if group #1 matched (with the(?1.....)
syntax), and if it does it inserts the text immediately following the?1
. It does a very similar thing for group #2 (inserting a different string of text if group #2 was matched). Thus you get different replacement text when group #1 (your0
) matches than you do when group #2 (your1
) matches.Hopefully this is somewhat understandable. All you have to do is change the replace-what box appropriately to get what you need. I could have done it but perhaps you learn more if you experiment with it. :-D
-
Another thing worth pointing out is that you don’t have to do BOTH replacements at the same time. Some situations actually demand this “same time” behavior, but yours doesn’t. This simplifies the situation with the regular expressions a good deal. For example,
Do:
Replacement operation #1:
Find-what:; Head Change\RT0\R
Replace-with:$0this\r\nfollows\r\na T-ZERO!\r\n
Then follow that with:
Replacement operation #2:
Find-what:; Head Change\RT1\R
Replace-with:$0THIS\r\nFOLLOWS\r\nA T-one!\r\n
Again, customize your replacement text at will!
-
Thank you Scott
I will test your recomendations right now. -
It works almost as expected when T1 (updated the code and in spanish coments) from your script.
;LAYER:150
G92 E0
G1 F2400 E-16.00000
; CAMBIO DE CABEZAL
T1
; Cambio T0 a T1
G1 F12000
G1 X245 Y50
G1 X245 Y0
G1 X225 Y20
G1 X300 Y0
G1 X300 Y50
; DESPUES DE CAMBIO DE CABEZALBut, when the line is T0, it add again the T1 line after the correct insert.
;LAYER:200
G92 E0
G1 F2400 E-16.00000
; CAMBIO DE CABEZAL
T0
; Cambio T1 a T0
G1 F12000
G1 X300 Y50
G1 X300 Y0
G1 X280 Y20
G1 X245 Y0
G1 X245 Y50
; Cambio T0 a T1
G1 F12000
G1 X245 Y50
G1 X245 Y0
G1 X225 Y20
G1 X300 Y0
G1 X300 Y50
; DESPUES DE CAMBIO DE CABEZALI need to do in one operation, because it will run in a external postprocesor utility.
Find input: ; CAMBIO DE CABEZAL\RT(?:(0)|(1))\R
Replace input: $0(?1; Cambio T1 a T0\r\nG1 F12000\r\nG1 X300 Y50\r\nG1 X300 Y0\r\nG1 X280 Y20\r\nG1 X245 Y0\r\nG1 X245 Y50\r\n)(; Cambio T0 a T1\r\nG1 F12000\r\nG1 X245 Y50\r\nG1 X245 Y0\r\nG1 X225 Y20\r\nG1 X300 Y0\r\nG1 X300 Y50)\r\n
-
My fault, typping bad. It works as expected!
Missing the (?2 before the second replace.
Thank you for your help.
Marcos