new notepad++ user, need help editing g code



  • I need to edit G code files to find all lines with “A” in them and add a new line “M1231” before that line and a new line “M1230” after.

    Heres an example, If i open a file that looks like this:

    G0 X5 y5
    G0 A180
    G0 X-5 Y-5
    G0 A0

    I need to change it to this:

    G0 X5 y5
    M1231
    G0 A180
    M1230
    G0 X-5 Y-5
    M1231
    G0 A0
    M1230

    Can notepad do this? Any help is much appreciated



  • @Quinn-RC said:

    Can notepad do this? Any help is much appreciated

    Yes. Notepad++ has regular expression syntax available for search-and-replace. It’s worth your time to learn. See the FYI section below for useful links.

    • FIND = (?-i)^.*A.*$
      • (?-i) : turns off case-insensitive (so matches case, regardless of checkboxes elsewhere)
      • ^: start match at beginning of row
      • .*: search for 0 or more characters
      • A: literal uppercase A
      • $: to the end of the line
      • in all, does a case-sensitive match, looking for beginning of row, any number of characters, a capital A, any number of characters, to the end of the line (not gobbling the EOL character(s))
    • REPLACE = M1231\r\n$0\r\nM1230
      • M1231: literal text
      • \r\n: windows line ending (CR and LF)
      • $0: the entire match from the FIND
      • M1230: literal text
      • in all, replaces the found line with M1231, a windows newline, the found line, a windows newline, and M1230
    • SEARCH MODE = regular expression

    -----
    FYI:

    This forum is formatted using Markdown, with a help link buried on the little grey ? in the COMPOSE window/pane when writing your post. For more about how to use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end. It is very important that you use these formatting tips – using single backtick marks around small snippets, and using code-quoting for pasting multiple lines from your example data files – because otherwise, the forum will change normal quotes ("") to curly “smart” quotes (“”), will change hyphens to dashes, will sometimes hide asterisks (or if your text is c:\folder\*.txt, it will show up as c:\folder*.txt, missing the backslash). If you want to clearly communicate your text data to us, you need to properly format it. That topic also explains how to embed images by uploading them to a public server like imgur.com, and embedding them using the syntax ![](http://i.imgur.com/QTHZysa.png)

    If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study this FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

    Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.



  • @PeterJones said:

    M1231\r\n$0\r\nM1230

    perfect! thank you very much. I will definitely follow your links to learn to do this stuff for myself, but i very much appreciate the included answer.


Log in to reply