I have a G code file for a CNC machine that needs to have two lines inserted between patterns. Essentially I need to write a simple post processor. Here is a small snippet of code from my CAM program:
G00 X-1.663 Y-0.992 Z+0.
G01 X-1.072 Y-0.992
G01 X-1.072 Y-0.874
G01 X-1.663 Y-0.874
G01 X-1.663 Y-0.992
G00 X-3.021 Y-0.992
G01 X-3.021 Y-0.874
G01 X-2.43 Y-0.874
G01 X-2.43 Y-0.992
G01 X-3.021 Y-0.992
I need to insert two lines between a G00 and G01 in that order. So the above example would now become:
G00 X-1.663 Y-0.992 Z+0.
M51
M04 F.1
G01 X-1.072 Y-0.992
G01 X-1.072 Y-0.874
G01 X-1.663 Y-0.874
G01 X-1.663 Y-0.992
G00 X-3.021 Y-0.992
M51
M04 F.1
G01 X-3.021 Y-0.874
G01 X-2.43 Y-0.874
G01 X-2.43 Y-0.992
G01 X-3.021 Y-0.992
Another thing I need is the line breaks must be Windows/DOS style with \r\n at the ends. This has to do with the old, fickle CNC controller which wants DOS text files. I could use yet another awk or sed script but a one liner would be preferred. I was trying:
awk '/G00/,/G01/{print $0 RS "M51" RS "M04 F.1";next}1' FILE
But the lines are added after ANY G00 or G01. I need ONLY between G00 and G01. One more thing, I would prefer single line commands. Thanks!
Using awk you can do:
awk '$1=="G00"{p=1} p && $1=="G01"{print "M51"; print "M04 F.1"; p=0} 1' file
G00 X-1.663 Y-0.992 Z+0.
M51
M04 F.1
G01 X-1.072 Y-0.992
G01 X-1.072 Y-0.874
G01 X-1.663 Y-0.874
G01 X-1.663 Y-0.992
G00 X-3.021 Y-0.992
M51
M04 F.1
G01 X-3.021 Y-0.874
G01 X-2.43 Y-0.874
G01 X-2.43 Y-0.992
G01 X-3.021 Y-0.992
You could use something like this:
awk 'last=="G00" && $1=="G01" {print "M51\nM04 F.1"} {last=$1} 1' file
If the first field of the previous line was "G00" and the first field of the current line is "G01", then print the two lines.
Output:
G00 X-1.663 Y-0.992 Z+0.
M51
M04 F.1
G01 X-1.072 Y-0.992
G01 X-1.072 Y-0.874
G01 X-1.663 Y-0.874
G01 X-1.663 Y-0.992
G00 X-3.021 Y-0.992
M51
M04 F.1
G01 X-3.021 Y-0.874
G01 X-2.43 Y-0.874
G01 X-2.43 Y-0.992
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With