bob24

Honorable Member
Joined
Feb 15, 2018
Messages
30
Is it possible to alter 2 lines in notepad using edit/replace?
I have a number of files, gcodes, that are in the format
G00 Xnn Ynn
G01 Xnn Ynn
G01 Xnn Ynn
G00 Xnn Ynn
G01 Xnn Ynn
G00 Xnn Ynn etc
I would like to finish up with a line before each G00 Xnn Ynn line with G00 Z2 and before each G01 Xnn Ynn line with G00 Z0 like this
G00 Z2
G00 Xnn Ynn
G00 Z0
G01 Xnn Ynn etc
Is this possible, maybe by using something in the replace box like G00 Z2 /n G00 Xnn Ynn where the /n is 'carriage return - line feed'?
I could do it manually but some of the files are hundreds of lines long.
 

Solution
You won't be able to do that using Windows Notepad's simple Find/Replace functionality—the built‐in editor doesn't support inserting newline characters (or using regex) in the Replace field. Notepad simply doesn't have that level of text processing capabilities.
However, you can achieve what you need using an editor that supports regular expressions and extended search/replace functions. For example, Notepad++ is a free editor that allows you to use special characters like \r\n (carriage return and line feed) in the replacement text.
Below are some sample methods for Notepad++:
  • To Insert "G00 Z2" Before Every Line Starting with "G00":
    1. Open your file in Notepad++.
    2. Press Ctrl+H to open the Replace...
You won't be able to do that using Windows Notepad's simple Find/Replace functionality—the built‐in editor doesn't support inserting newline characters (or using regex) in the Replace field. Notepad simply doesn't have that level of text processing capabilities.
However, you can achieve what you need using an editor that supports regular expressions and extended search/replace functions. For example, Notepad++ is a free editor that allows you to use special characters like \r\n (carriage return and line feed) in the replacement text.
Below are some sample methods for Notepad++:
  • To Insert "G00 Z2" Before Every Line Starting with "G00":
    1. Open your file in Notepad++.
    2. Press Ctrl+H to open the Replace dialog.
    3. In the "Search Mode" section, select "Regular expression" (or "Extended" if using escape sequences).
    4. In the "Find what" field, enter:
      Code:
            ^(G00 X)
      This regular expression matches any line starting with "G00 X".
    5. In the "Replace with" field, enter:
      Code:
            G00 Z2\r\n\1
      This tells Notepad++ to insert "G00 Z2" and then a new line before the matched string (where \1 represents the original "G00 X").
    6. Click "Replace All".
  • To Insert "G00 Z0" Before Every Line Starting with "G01":
    1. Again in the Replace dialog (or in a separate command), enter in "Find what":
      Code:
            ^(G01 X)
    2. In "Replace with", enter:
      Code:
            G00 Z0\r\n\1
    3. Click "Replace All".
If your file formats or the parts you need to replace vary a bit, you may need to tweak these regular expressions further. Notepad++ also supports more advanced regex patterns if necessary.
In summary, while Windows Notepad is too limited for this kind of multi-line manipulation, using an enhanced editor like Notepad++ will allow you to automate the insertion of new lines and the adjustment of your G-code files effectively. Happy editing!
— ChatGPT
 

Solution
Notepad’s Replace can’t insert new lines, but using Notepad++ with regex mode, you can insert G00 Z2\n before G00 and G00 Z0\n before G01 lines efficiently
 

Absolutely correct! Notepad's basic Replace function doesn't support special characters like new lines, but Notepad++ allows powerful searching and replacing with regular expressions (regex), including inserting line breaks or additional commands.
Here’s a quick step-by-step for others who want to do this:

To insert G00 Z2 before every G00 line and G00 Z0 before every G01 line using Notepad++:​

  1. Open your file in Notepad++.
  2. Press Ctrl + H to open the Replace dialog.
  3. At the bottom, select "Search Mode: Regular expression".
  4. For G00 lines:
    • Find what: ^(G00.*)
    • Replace with: G00 Z2\r\n\1
    • Click "Replace All".
  5. For G01 lines:
    • Find what: ^(G01.*)
    • Replace with: G00 Z0\r\n\1
    • Click "Replace All".
Note:
\r\n inserts a new line (Windows format). If you’re on Linux or Mac, use \n instead.
This approach efficiently inserts your desired lines before every matching line—saving tons of manual effort. Notepad++ is great for these sorts of batch changes!
If you want a custom regex for more complex scenarios or want help automating this further, feel free to upload a sample file or ask for code!
 

Back
Top